# Looping Actionscript

**URL:** <https://forum.kirupa.com/t/looping-actionscript/6381>\
**Category:** flash\
**Created:** [October 5, 2002, 9:40pm UTC](https://forum.kirupa.com/t/looping-actionscript/6381 "2002-10-05T21:40:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bcogswell11](https://avatars.discourse-cdn.com/v4/letter/b/8e7dd6/32.png) [@bcogswell11](https://forum.kirupa.com/u/bcogswell11)\
**Post date:** [October 5, 2002, 9:40pm UTC](https://forum.kirupa.com/t/looping-actionscript/6381/1 "2002-10-05T21:40:40Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 5, 2002, 11:45pm UTC](https://forum.kirupa.com/t/looping-actionscript/6381/2 "2002-10-05T23:45:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 6, 2002, 7:24am UTC](https://forum.kirupa.com/t/looping-actionscript/6381/3 "2002-10-06T07:24:46Z")

</div>

OK, here is the code:

```auto
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 🙂 //pom

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 6, 2002, 8:09am UTC](https://forum.kirupa.com/t/looping-actionscript/6381/4 "2002-10-06T08:09:17Z")

</div>

This traces a number every 3 seconds:

```auto
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:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 6, 2002, 8:19am UTC](https://forum.kirupa.com/t/looping-actionscript/6381/5 "2002-10-06T08:19:09Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 6, 2002, 9:22am UTC](https://forum.kirupa.com/t/looping-actionscript/6381/6 "2002-10-06T09:22:15Z")

</div>

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 =)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 6, 2002, 9:40am UTC](https://forum.kirupa.com/t/looping-actionscript/6381/7 "2002-10-06T09:40:00Z")

</div>

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

```auto
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:

```auto
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:
