Game: Algorithm for spawning enermies at game/window egdes

Go To


I'm looking for an algorithm for spawning enermies at the egdes for the gameboard/window for an Android game. I'm doing the old SpaceWars/Shooting asteriods with spaceship and i need the asteroids to spawn at random locations outside the gameboard, where after they will move into the gameboard in different directions and speeds.

It's easy to place the asteroids at random locations, but the tuff part (at least for me) is to give them the right move direction based on the random location they are giving.

For example:

Gameboard = 1280 x 720

Asteroid random location = 1100, 0 (x, y)

Right move direction:

asteroid.x < gameboard.x/2 - possible move directions = south and south east

asteroid.x > gameboard.x/2 - posiible move directions = south and south west

Then the asteroid have two possible move directions (that's how i define the gameplan for now): Direct south or south west.

I could do a lot of if statements, but i'm hoping someone can help me with some kind of algorithm for the problem?

2012-04-04 17:54
by Poku
What determines the "right move direction" - jonmorgan 2012-04-04 18:08
i have edited the "right move direction" definitio - Poku 2012-04-04 18:18
how are you defining direction? string? int? don't know yet - JRaymond 2012-04-04 18:24
they are defined as enums with int values - Poku 2012-04-04 20:03


Maybe I got wrong your question but this is how I see the solution :

If the asteroid x-position exceeds your device width (720) then its direction should be SW otherwise it should be S.

2012-04-04 18:05
by Ovidiu Latcu
yes, this is how i would do my self, but there's gonna be a lot of if statements and i thinking if i could avoid that - Poku 2012-04-04 18:19


This link may help you

I think you might always need some nested if statements but this way may be a bit more clever

2012-04-05 15:10
by jacob