ForumsProgramming ForumHelp with randomness please on AS3

4 3647
P051D3N
offline
P051D3N
25 posts
Nomad

Hi everyone, this is my first post here, and I am quite new to actionscript (although I have been working on gamemaker 8 without coding for some time). I am trying to make a basic target shooting game to start off with but so far it looks very rigid and pre-set so I decided to add some randomness. If anyone could explain in noob language how to
a. make a movie clip jump to a random "y" location (between 1 and 400).
and b. set a timer to go off at a random time between 2000 and 5000 millisecond (I already know how to make basic timers)

Thanks in advance for all your help!

  • 4 Replies
PixelSmash
offline
PixelSmash
566 posts
Nomad

This stuff is not that hard... you probably could've found it on google as well

anyway, what language are you aiming at? AS2 or AS3? For AS2 I'm probably not the right person... but I can give you some stuff for AS3

a: movieclip.y = 1 + Math.random()*399 (0 - 400 would be Math.random()*400)

b: timer = new Timer(2000 + Math.random() * 3000);
if you want to change it for each cycle, use timer.delay = 2000 + Math.random() * 3000 in the TimerEvent.TIMER handler

Good luck!

P051D3N
offline
P051D3N
25 posts
Nomad

Thanks, and yes I did want AS3.

P051D3N
offline
P051D3N
25 posts
Nomad

Sorry, I don't understand the second bit of part b. Could you give me an example of code with the second part of b in it.
Thanks

PixelSmash
offline
PixelSmash
566 posts
Nomad

Sure, here we go!

//create a timer with a delay between 2000 and 5000 ms, which runs forever
timer = new Timer(2000 + Math.random()*3000);

//add the event listener, the function the timer calls every time the timer goes off
timer.addEventListener(TimerEvent.TIMER, timerTick);

//start the timer, ofcourse
timer.start();

//the function the timer calls
private function timerTick(e:TimerEvent):void
{
trace("tick!"
}

So, this is a 'static' delay - the timer's delay is set just once, and never changes. If you want it to change each time the timer goes off, just add the following line to timerTick, and it will be randomized after each time it goes off:

timer.delay = 2000 + Math.random() * 3000;

Hope that makes it a bit more understandable!

Showing 1-4 of 4