[Flash MX 2004] Issues with random numbers and movie clips

Associate
Joined
21 Sep 2007
Posts
453
I am having some very odd issues with a randomly rotating image banner.

The setup is very basic. 6 key frames, the first being empty and the next 5 populated with one movie clip per frame.

Code on the first frame:

Code:

Code:
rand = Math.floor(Math.random() * (7-2)) + 2;

gotoAndPlay(rand);

trace(rand);

This generates a random number ranging from and including 2 to 6 (the frames containing the clips)

Then, frames 2-6 have a stop(); command on them.

Lastly, inside the movie clips, there is a play(); command on the first frame and on the very last frame of each, I have this code:

Code:

Code:
a=Math.floor(Math.random() * (7-2)) + 2;

while(a==_root._currentframe){
	a=Math.floor(Math.random() * (7-2)) + 2;
}
_root.gotoAndStop(a);


Now, heres the kicker. Often, the flash works fine, although it does like to favour frames 2, 3 and 5. Other times, on executing the swf, the randomly chosen clip fails to play, despite the play(); command.

Using the trace, I noticed it was trying to load and play frame 4, yet when I right clicked the flash and told it to play, the clip to load was the clip from frame 5!

I doubled checked all the code, and there is nothing out of place, all syntax is correct and i have used the same numbers in all the random generation code.

Can anybody shed some light on this very odd problem?

Thankyou!
 
i just went ahead and made a working example using a function, my brain has it's blind moments and atm i can't see the fault either :o if it dawns upon me i'll be back but just so you're not at a dead end:

http://homepage.ntlworld.com/dabar/ocuk/flash/randNum.fla
AS2.0:
frame one on the root timeline..
Code:
stop();
function fRandFrame():Void {
	var min:Number = 2;
	var max:Number = 6;
	do {
		var nRandNum:Number = Math.floor(Math.random()*(max-min+1))+min;
	} while (nRandNum == _root._currentframe);
	_root.gotoAndStop(nRandNum);
}
fRandFrame();

inside movieClips:
frame one..
Code:
play();
last frame..
Code:
gotoAndStop(1);
_root.fRandFrame();
and thats it.
 
just change your code to...

Code:
rand = random(5)+1;
gotoAndPlay(rand);

that should work

this does not fix the problem with the one MC being skipped. Changing the number to 1 means that it will choose between frames 1 and 5, frame 1 being empty apart from some the random number code.

Hector, I will try your code now and post results.

thanks
 
ok, tried Hectors code, and guess what, it goes mental when it starts off trying to goto frame 4!!!

other then that its fine. I havent a clue whats wrong, i C&P'd the code exactly, its in all the correct places.

When it generates the number 4 (to goto frame 4) the flash simply doesnt start. Right click and telling it to play, the flash jumps through about 3 of the clips and then behaves normally.

Very very odd!

Any ideas?
 
Hector - You code worked perfectly in Flash MX 2004 (the version im using)

Yet the project i made from scratch is still bugged.

So I decided to import my movie clips into your file, and replaced each one, making sure all code was correct. I even changed my clips to use two layers, one for code bits and one for the graphics.

Replacing 1 or 2 clips, everything worked fine. Yet after replacing them all, the thing bugged out again, on, your guessed it, frame 4!

I have attached a link to my project (mx2004) for you to look over. I am 100% certain my code is exactly the same as yours and in the correct places.

Do you think the update could fix this damn frame 4???

Thankyou


Flash MX 2004 Project:

DivShare File - new_banner.fla
 
it's working fine for me :confused:
Code:
clip2
clip4
clip5
clip1
clip3
clip2
clip4
clip5
the update, i don't know for sure but it'll fix a lot of other issues before you find them, so it's worth a try.

i'll do a version using a setInterval to remove the need for the image movieClips and i'll upload the fla.. another method may get around this problem.


EDIT:
http://homepage.ntlworld.com/dabar/ocuk/flash/new_banner_interval.fla
only the first frame of the movie has code :cool:
Code:
function fRandFrame():Void {
	var min:Number = 2;
	var max:Number = 6;
	do {
		var nRandNum:Number = Math.floor(Math.random()*(max-min+1))+min;
	} while (nRandNum == _root._currentframe);
	trace("Frame: "+nRandNum)
	_root.gotoAndStop(nRandNum);
}
var nDisplaySeconds:Number = 2;
var nImageInterval:Number = setInterval(this, "fRandFrame", nDisplaySeconds*1000);
stop();
fRandFrame();
 
Last edited:
Hector - Sorry i dont think i was too clear.

If you try running the flash file a few times, wait till it generates the number 4 on the first go (in your quoted bit, it started on clip2 (frame 3)

When it does start on frame 4, it should show as blank, as the file is paused (for unknown reasons) and using the flash controls (right click) to play it, it will skip to frame 5 (clip4)

Hope this helps in trying to replicate the effect
 
ah yes i see what you mean now and i have a theory.. i'm thinking it's the loading of the data with-in the flash player.

with the setInterval method, as long as the first number is <4 it'll display the correct frame but if >=4 it only shows frame 4 because in my theory frames 5 and 6 have yet to be loaded.

i'll attempt to fully preload the movie to test this theory but i won't get time 'till later tonight as bro's over and wants to look at pc hardware for his upgrade.

EDIT: quick test.. fully preloaded, not had time to repeat test it but first attempt frame 7 loaded correctly :)
http://homepage.ntlworld.com/dabar/ocuk/flash/new_banner_interval_preload.fla
 
Last edited:
Back
Top Bottom