Looping Actionscript

Hey guys,
I am working on a code that beigns with

onClipEvent(enterFrame) {

After that, it chooses a random number between 1 and 4, if the number is 1 or 2, it goes through some directions, however, if doesn’t, I have written some code to wait three seconds using

getTimer();

After I use the getTimer code, how can I make it go back to the beginning of the onClipEvent to redo the script again and pick the random numbers again?

It might make it a little trickier, but I also have an

onClipEvent(load)

before the enterFrame code. Is this possible?

Thanks!
-brad-:cowboy:

Wouldn’t it be easier if you posted the code? :slight_smile:

OK, here is the code:

onClipEvent(load) {
	homer = 0;
	pause = 3000;
	elapsedTime = getTimer();
	this.gotoAndStop(1);
}
onClipEvent(enterFrame) {
	generator = Math.round (Math.random()*4+1);
	if (generator == 1) {
		if (getTimer() - elapsedTime >= pause) {
		          [COLOR=darkblue]//Redo the random number picking;[/COLOR] 
		}
	}
	if (generator == 2) {
		if (getTimer() - elapsedTime >= pause) {
		          [COLOR=darkblue]//Redo the random number picking;[/COLOR] 
		}
	}
	if (generator == 3) {
		homer = 1;
		gotoAndPlay(2);
	}
	if (generator == 4) {
		homer = 1;
		gotoAndPlay(2);
	}
}

I want the script to repick random numbers after it goes thru the timer, wihch lasts three seconds. How do I make it loop back to the part where it chosses the random number?

Thanks!
-brad-:cowboy:

BTW: I don’t know how to contain the script in the special code box, so if someone wants to edit my post it would be appreciated!

Edit: done :slight_smile: //pom

This traces a number every 3 seconds:

onClipEvent(load) {
	homer = 0;
	pause = 3000;
	elapsedTime = getTimer();
	this.gotoAndStop(1);
}
onClipEvent(enterFrame) {
	if (getTimer()-elapsedTime >= pause){
		var generator = Math.round (Math.random()*4+1);
		if (generator==1||generator==3) {
			elapsedTime = getTimer();
			trace (generator);
		}
		if (generator==2||generator==4) {
			elapsedTime = getTimer();
			trace (generator);
		}
	}
}

pom :asian:

Ilyas, thanks for your reply. How do you do the code thingy?

Anyways, you will see on mine that the timer is part of an “if” statement depending on the random number picked. The code you wrote contains the “if” statement based on the timer. Will it still work if I change the order around to suit my needs?

Thanks!
-brad-:cowboy:

Maybe it would be easier to use setInterval()!
use
MovieClip.protoype.myaction = function() {}
or
function myaction(){}
and put
setInterval(myaction,3000);
between
onClipevent(enterFrame) {}
handlers
eki posted this nifty little action a while ago =)

Yep, setInterval would be the way to go with MX. I put the thread in the best of kirupa if you want to check it.

Concerning the code I wrote, the

if (getTimer()-elapsedTime >= pause){

part will make the movie pick a random number every 3 seconds. Then you can do whatever you want with it…

The problem with your code:

onClipEvent(enterFrame) {
	// you generate a new number
	generator = Math.round (Math.random()*4+1);
	if (generator == 1) {
		// this is false, Flash gets out of the loop
		// picks another number... you never wait
		if (getTimer() - elapsedTime >= pause) {
		          //Redo the random number picking; 
		}
	}

pom :asian: