Skip to main content
Known Participant
December 1, 2010
Answered

random timer interval

  • December 1, 2010
  • 2 replies
  • 4193 views

I'm trying to build a scene in which multiple instances of the same mc start at different intervals and continue to restart over and over again at different intervals.  I'm trying to get the mc to start anywhere from 3 to 10 seconds after it last stopped.  I used this as3 code inside th mc itself.

var randNum:Number;

randNum = Math.round(Math.random() *5 + 2*1000);

trace(randNum);

var timer:Timer = new Timer(randNum);

timer.addEventListener(TimerEvent.TIMER, randGen);

function randGen(e:TimerEvent):void{

trace("Hello");

}

timer.start();

The trace shows a random number is generated but it doesn't seem to effect the interval of the timer after the first run through.  "Hello" shows up in the output window every second after the randNum is traced regardless of the randNum's value.

I've tried using timer.reset but that seems to do nothing at all or stops the repeat.  Probably because I don't understand how to use either '.reset' or '.delay'.  I looked them up in the documentation and still don't get how they are used or even if they are appropriate to this situation.  Obviously, a lot I do not understand about as3.  Will someone kindly point out what I'm doing wrong?  Thanks.

This topic has been closed for replies.
Correct answer journeydude

If you are insistent on using it the way you have it set up, perhaps you could try doing something like this:

var randNum:Number;

randNum = Math.round(Math.random() * 5 + 2000);

trace(randNum);

var timer:Timer = new Timer(randNum);

timer.addEventListener(TimerEvent.TIMER, randGen);

function randGen(e:TimerEvent):void {

var timerObj:Timer = e.target as Timer;

randNum = Math.round(Math.random() * 5 + 2000);

trace("New delay value:",randNum);

trace("Hello");

timerObj.delay = randNum;

}

timer.start();

As the previous poster mentioned if you want a more graduated random delay be careful of what you are using for the random number. Math.random() generates a floating point number between 0 and 1 (e.g.: 0.65687651151), so Math.random() * 5 is going to be a portion of the whole number of 5. Adding 2000 to that is only going to give a number between 2000 and 2005.

Hope that helps!

2 replies

journeydudeCorrect answer
Inspiring
December 1, 2010

If you are insistent on using it the way you have it set up, perhaps you could try doing something like this:

var randNum:Number;

randNum = Math.round(Math.random() * 5 + 2000);

trace(randNum);

var timer:Timer = new Timer(randNum);

timer.addEventListener(TimerEvent.TIMER, randGen);

function randGen(e:TimerEvent):void {

var timerObj:Timer = e.target as Timer;

randNum = Math.round(Math.random() * 5 + 2000);

trace("New delay value:",randNum);

trace("Hello");

timerObj.delay = randNum;

}

timer.start();

As the previous poster mentioned if you want a more graduated random delay be careful of what you are using for the random number. Math.random() generates a floating point number between 0 and 1 (e.g.: 0.65687651151), so Math.random() * 5 is going to be a portion of the whole number of 5. Adding 2000 to that is only going to give a number between 2000 and 2005.

Hope that helps!

VideoEAuthor
Known Participant
December 2, 2010

And thank you for the 2nd half of the puzzle, to regenerate a random number each time! Yeah.

I kept on trying 'timer.delay(randNum)' and it didn't work.  This code does.  But I don't get it: var timerObj:Timer = e.target as Timer;  So you have to create a new Timer object to receive the delay method/ parameters?  And where does the 'e.target as Timer' code come from?   And why can't you just use the .delay method with the original 'timer' object?

I'm not a coder,(bet you never guessed), but this language logic gets weird at every turn I make.  AAARRRGGGHHH!!!!

OK, I feel much better now...  And aagain, thank you.

Inspiring
December 2, 2010

No problem. Glad to be of service

December 1, 2010

I think it's working as it should - the problem is you're generating a number only between 2000 and 2005...

randNum = Math.round(Math.random() *5 + 2*1000);

Is the same as:

randNum = Math.round(Math.random() *5 + 2000);

If you want it between 3 and 10 seconds you need 3000 - 10000:

randNum = Math.round(3000 + Math.random() * 7000);

VideoEAuthor
Known Participant
December 2, 2010

Thanks for reminding me I've gotten progressively lousier at math!  You are, of course, right and your math gave me the proper interval range.  thanks.