• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

Newbie in Action: Random Scripting Question

I created a small banner.
468x60.

I have a small movieclip which is a animated star.. I want to display it randomly on my banner from time to time.

Not entirely sure how to do this..

Any help would be great..

0
rurth24
Asked:
rurth24
  • 14
  • 12
  • 8
2 Solutions
 
BillystyxCommented:
if(random(20)==2){
attachMovie("mystar","mystar",2);
}

Billystyx
0
 
rurth24Author Commented:
Where do I attach this code?
In the movie clip on the first frame?

or

in the main time line?
0
 
BillystyxCommented:
in the main timeline, first frame (I guess - if that's where you want to start the star animatioon), and if the star mc is in the main swf, then set the linkge identifier of it to, for example, 'mystar', and then add the above code to that first frame. If themain swf loops, then put this first:

if(!_root.mystar){
 if(random(20)==2){
attachMovie("mystar","mystar",2);
}
}

Billystyx
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
CerfCommented:
you can also change the position of it randomly by using the following code:

_root.mystar._x=random(your_banner_width)
ie: _root.mystar._x=random(350)

_root.mystar._y=random(your_banner_heigth)
ie: _root.mystar._y=random(100)

Cërf
0
 
rurth24Author Commented:
I placed the my movie (instance name mystar) on the first frame of main time line.
In that same frame I placed the below action.. Doesn't seem to be working? What am I doing wrong?


if(!_root.mystar){
 if(random(20)==2){
attachMovie("mystar","mystar",2);
}
}
0
 
rurth24Author Commented:
Thats what I wanted, sorry if I didn't explain that in the first posting..
I want it to display randomly all over the banner..

0
 
rurth24Author Commented:
I want it to look like twinkling stars...
0
 
CerfCommented:
Well, you need to open the library (CTRL + L) right click on the star movie clip, click on linkage, check "export for actionscript" then on the "identifier" texbox write "mystar" without the quotes, and then the code Billistyx gave you should be working like a charm...
0
 
CerfCommented:
sorry, what's "twinkling stars"?... English not my first language
0
 
rurth24Author Commented:
First off I did what you said about the linkage...

I keep getting this error

**Error** Scene=Scene 1, layer=Layer 13, frame=1:Line 5: Unexpected '}' encountered
     }

Total ActionScript Errors: 1        Reported Errors: 1

Second : "Twinkling Stars"  Like the Night Sky? I want the movie clip to play random all over the banner... so it looks like the night sky.
0
 
rurth24Author Commented:
Cerf you added in this


_root.mystar._x=random(468)
_root.mystar._y=random(60)

How do I add this to billy's code?

0
 
CerfCommented:
I made an example for you,
http://trabuca.com/cerf/

Take a look at it and let me know if that is what you wanted

Cërf.
0
 
BillystyxCommented:
if you want lots of stars, try this:(along with setting linkage identifier as cerf explained:

stars=[];
for(i=0;i<100;i++){
dep=_root.getNextHighestDepth();
attachMovie("mystar","mystar"+i,dep);
stars.push("mystar"+i);
_root["mystar"+i]._x=random(468);
_root["mystar"+i]._y=random(60);
}
setInterval(starson,500);
function starson(){
num=random(stars.length);
removeMovieClip(stars[num]);
stars.splice(num,1);
mcname="mystar"+(stars.length+1);
attachMovie("mystar",mcname,dep);
stars.push(mcname);
_root[mcname]._x=random(468);
_root[mcname]._y=random(60);
}

Billystyx
0
 
rurth24Author Commented:
Yep that's what I'm looking for...
0
 
BillystyxCommented:
aircode:) I will test it...
0
 
BillystyxCommented:
yes it works...
0
 
rurth24Author Commented:
Billy pasted what you had on the first frame of my mainStage...
I did the linkage like Cerf explained but nothing is working for me..

0
 
rurth24Author Commented:
Okay... I got it to work partially..

What it does is displays lots of stars, then it goes into a random pattern, for a few and displays lots of stars again.

0
 
CerfCommented:
take a llok at http://trabuca.com/cerf/stars.fla and see if that works as you want.
It is very simple.
The only code is
main stage [Frame 1]: attachMovie("mystar","mystar", 2+random(1000));

star mc
[frame 1]
this._x=random(468);
this._y=random(60)
[last frame]
this.removeMovieClip();

that's it
0
 
BillystyxCommented:
slight miscalculation in my script:
stars=[];
for(i=0;i<100;i++){
dep=_root.getNextHighestDepth();
attachMovie("mystar","mystar"+i,dep);
stars.push("mystar"+i);
_root["mystar"+i]._x=random(468);
_root["mystar"+i]._y=random(60);
numbs=stars.length;
}
setInterval(starson,500);
function starson(){
      numbs++;
      dep1=_root.getNextHighestDepth();
num=random(stars.length);
removeMovieClip(stars[num]);
stars.splice(num,1);
mcname="mystar"+(numbs);
attachMovie("mystar",mcname,dep1);
stars.push(mcname);
_root[mcname]._x=random(468);
_root[mcname]._y=random(60);
}

this is better.

The code should sit in its own keyframe on frame1 of your clip.
It shouldn't loop, so if the banner does, then on last frame of the banner, add a gotoAndPlay(2);
so that frame1 only gets run once.

Billystyx

cerfs is much more straight-forward, (even though arguably code alone is:), so try that one out too - see what suits you better.

Billystyx
0
 
rurth24Author Commented:
The doesn't loop, and the new code works great except the first time it loads I get like a hundred stars that display all at once then it goes into the random mode one at a time.

0
 
BillystyxCommented:
How did you want it to work?
How many stars up at first, how many disappearing?
Its easily tailorable ... just say:)
Billystyx
0
 
rurth24Author Commented:
I really only want a couple.

for(i=0;i<100;i++)

Do I tailor via this line?

for(i=0;i<2;i++)

???
0
 
CerfCommented:
do you want less stars?
easy, add more frames to the main timeline [at the end]
so the attachMovie("mystar","mystar", 2+random(1000)); gets called less times

I know this is like prehistoric code, but since it's not a highend application, I guess I can take a chance with it, since it does not even load the processor with much to do and the mc gets removed after it has been played : )

Cërf.
0
 
BillystyxCommented:
yeah, that's where you would do it - but I came up with something prettier - just a minute and I will try to upload for viewing...
Billystyx
0
 
rurth24Author Commented:
I used yours and I got it to work great.
Thanks Cerf..

I want to try Billy's also and get it to work...
I know his is alittle more complicated but this is also a learning process for me..
It's good to see things done differently..

I'm going to raise the point value and split it for both of you...
0
 
BillystyxCommented:
0
 
BillystyxCommented:
Again - you edit the number of stars by changing the number in the for loop - it is currently at 50.

Billystyx
0
 
BillystyxCommented:
As you say Cerf, there is nothing wrong with flash stuff that visibly shows what's happening - I am just lazy and not much good at graphics:)

I just came up with the idea that twinkling stars might look better fading in and out rather than switching on and off, hence my last update in the zip file.

I took out the function, and inside the mc of the star added 4 frames:
frame1:
num=random(40);
if(num==5){
      gotoAndPlay(3);
}else{
      this._visible=true;
}
alphadir="down";
frame2:
gotoAndPlay(1);
frame3:
if(this._alpha>=0 && alphadir=="down"){
this._alpha -=5;
}else if(this._alpha<=0 && alphadir=="down"){
      alphadir="up";
}
if(this._alpha<=100 && alphadir=="up"){
this._alpha +=5;
}else if(this._alpha>=100 && alphadir=="up"){
      alphadir="down";
}      
frame4:
gotoAndPlay(3);

Billystyx
0
 
rurth24Author Commented:
Thanks alot for all the help..
Billy your posting even though alittle more complicated worked... and I'm going to take a good look at how you scripted it..
I already had the movie clip fading in and out so I really don't think I'll need all those frames but it's good for the future...

I've been working in Web Design for almost 7 years... HTML, Coldfusion, Flash ( I have basic actionscript knowledge, Javascript (basic) etc.)
If you ever need help with graphics etc  feel free to email me...

rurth24@yahoo.com

Thanks again..

0
 
CerfCommented:
Well,
Billy,
The first fla was only to illustrate the code and how it would work, I just added a tweening that faded in and out  and uploaded it again.

rurth24, glad you got it working, let us know where the banner is, if it's going to be online so we can take a look at it : )

Cërf.
0
 
rurth24Author Commented:
No prob...
0
 
BillystyxCommented:
thanks all, that was fun:)
0
 
CerfCommented:
yep, it was!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 14
  • 12
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now