Reputation: 25
I basically have a bunch of enemies in my game stored in an array while they are alive. I want a friendly ship to pick a random target from the array of enemies and shoot it. For this I need the x and y coordinates of that randomly selceted enemy from the array as it is used in the following calculation for the bullets:
_endX = randomenemyship.x - 50 * _pcos + Math.random() * _bulletSpread - _bulletSpread * .5;
_endY = randomenemyship.y - 50 * _psin + Math.random() * _bulletSpread - _bulletSpread * .5;
Would anyone be able to help, Ive been looking at how to select a random array element but Im not sure how to get coordinates and use it in a calculation like above. Thanks.
Upvotes: 0
Views: 111
Reputation: 13839
To select a random element and get the x and y coordinates of it, you would do something like this:
var rand = arr[Math.floor(Math.random()*arr.length)];
trace(rand.x, rand.y);
// rand.x and rand.y are the coordinates
As for the calculation, make your own and try not to copy code directly from the internet and you will know where the following values get placed in it.
Upvotes: 1