Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

start with random frame

Status
Not open for further replies.

sarak

Programmer
May 3, 2001
25
US
Each time my movie loads, I would like it to start at a random frame. I've used the following code in my movie and it works just fine in flash player. I'm using Flash MX and publishing it in Flash 5 format. However, in my browser, my movie starts at the beginning every time.

Here's what I have in the first frame of my movie after the preloader:

Code:
x = (13 + Math.floor(Math.random()*(280-13+1)));
gotoAndPlay(x);

The random-ness doesn't appear to be the problem since it still doesn't work with I simply put

Code:
gotoAndPlay(230);

Any ideas as to what's going wrong? Thanks!
 
Why not put that code as completion statement in the preload loop (i.e. IF all of the movie is loaded, go to frame (random) and play). Sounds like you have "IF all of the movie has loaded, go to frame X", where frame X contains the above script. No point sending the timeline to the instructions about where to send the timeline now, is there?! _____________________________________________________
Knowledge is attained only by seeking out that which is unknown
 
Works fine for me! Assuming the generated number(you can trace it with trace(x); ), exists in the movie. If it doesn't it should go to the last existing frame of your movie. Regards,

oldman3.gif
 
I still use the old "random()". It works as well or better than the new "Math.random()" even in MX. Just put

YourMovie.GoToAndStop(random(Number of Frames +1));

The only problem I have not resolved yet, is that sometimes the random comes up several times with the same number. Does anybody know how to prevent this?
 
You can at least store the current "pseudo" ramdom generated number in a variable (prev_random = x), and compare it on the next call (before sending it to the frame) to the current number, and if equal, generate a new number. That way, chances are, you won't get the same number at least twice in a row, which as BBD as suggested, is in fact a random number.

You could also each time, store the generated number in an array, compare the current number with all the ones in the array, and generate a new number if a match is found. Regards,

oldman3.gif
 
After giving up in frustration a couple of months ago, I have returned to this problem. (THANK YOU oldnewbie, BigBadDave, frihi and rgstewart for posting responses!).

I'm tempted to think that I'm encountering a bug in flash MX with the gotoAndPlay action. Below are the links to both my swf and fla files. Although the action gotoAndPlay(250); is in the main timeline, it's getting stuck between the first and second movie clips. Any ideas?? I'm not giving up this time! :)

SWF: FLA:
 
Works for me!
Added a frame identification on frame 250, and change your goto to a gotoAndStop(250);. Sure enough, it went to frame 250 and stopped! Regards,

oldman3.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top