Hey. So, what I'm trying to do is create an enemy at a random position, but inside these coordinates:
X: Between -30 and 1580
Y: Between -1045 and 2250
The code should look like this:
Code:
tEnem.x= //random between -30 and 1580
tEnem.y= //random between -1045 and 2250
I've been Googling and attempting for two hours, and I'm getting frustrated. Could any experienced programmers point me in the right direction? Thanks in advance.
Try below, Math.random gets number between 0 and 1, so if you use it like Math.random()*3, it will return 0,1,2. In the first example, it gets numbers 0-1580, then we subtract 30 to make the minimum value -30 and highest value 1580. I think that explanation is correct. I rarely use Math.random with such large numbers in my applications, so my experience is limited in that respect.
var randomNum:int = Math.random()*(1581+30)-30; // Integer, whole numbers, no decimals
var randomNum:Number = Math.random()*(2251+1045)-1045; // Number with trailing decimals
Use either int or Number, depending on what you want, i would use int for whole numbers.