I’m trying to distribute 3 objects randomly on my stage but it’s not working. My movie is 800×800.
function makeRock():void{
var tempRock:MovieClip;
for(var i:Number = 1; i < 3; i++){
tempRock = new Rock();
tempRock.x = Math.round(800);
tempRock.y = Math.round(-800);
addChild(tempRock);
}
}
What am I doing wrong?
Replace
Math.round(800);withMath.random()*800;Math.round(800)is just returning 800.Math.random()returns a random number between 0 and 1, which you can multiply by 800 to get a random result of 0-800. A good note to make is thatMath.random()never actually returns 1.0. Just everything from 0 up to 1.Further reading:
As a side note: this makes it simple to return a random element from an array; because you’re never getting 1 you can cast the result of
Math.random()*array.lengthtouint()and always be within the boundaries of the array length.eg.