Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by ZachRas

Is it not possible to use Math.Random()? and if not is there some implementation of it that I can use?

I'm pretty new to JavaScript so I apologize for the noob question.

1 decade ago by mtg101

I've been using Math.random() in iJS without any issues. Note that it's a lower case r in random() -- I noticed in your post that you've got it capitalised.

1 decade ago by lTyl

Math.random() returns a value between 0 and 1. If you want only whole numbers, then you have to use something like this:
Math.floor((Math.random()*20)+1);

.floor() will round the passed number down, so 14.9 will become 14. Math.random()*20 is basically: a random number between 0 and 1 multiplied by 20, and then add 1. Which returns a random number between 1 and 20.

If you want to round-up instead of down, use Math.ceil() instead of .floor().

1 decade ago by ZachRas

I figured it out...

I had

Math.floor((Math.random()*10)+1);

Instead of

powerup: Math.floor((Math.random()*10)+1),

Comma messed me up. Thanks for the replies! I've just started with impact and am loving it.
Page 1 of 1
« first « previous next › last »