# Setting 4 different enemy spawn zones off screen

This is my first game, so I really appreciate all the help.

I want to create four enemy spawn zones for enemies and this areas are off screen. The enemies are coming from left, right, up and down, and their destination is a sprite on the center of the screen. And there are 4 types of enemies (only thing that changes is color).

Current code I have only makes the enemies come from the right direction into the screen. I don't really know the reason of this, I am really new to game making and some of this methods.

Relevant code I have for enemies if the following:

private void createEnemies() {
Random ran = new Random();
int z = ran.nextInt(3 - 0 + 1) + 0;
Sprite ship = loadSprite(100, 100, listP.get(z));
attachChild(ship);
Enemies enemy = new Enemies(ship);

}

@Override
protected void onManagedUpdate(float pSecondsElapsed)   {
Random ran = new Random();
int z = ran.nextInt(3 - 0 + 1) + 0;
super.onManagedUpdate(pSecondsElapsed);
timeEnemies +=  pSecondsElapsed;        //  Acumulates time
if  (timeEnemies>TIME_LIMIT)    {   //  Time has completed
timeEnemies =   0;
Sprite spriteEnemy  =   loadsprite(GameControl.CAMARA_WIDTH+listP.get(z).getWidth(),
(float)(Math.random()*GameControl.CAMARA_HEIGHT-listP.get(z).getHeight())   +
listP.get(z).getHeight(),listP.get(z));

Enemigos newEnemy = new Enemies(spriteEnemy);
//newEnemy.mover(0,10);
listEnemies.add(newEenemy); //  Adds it to the scene
attachChild(newEnemy.getEnemySprite()); //  adds to list

}

//  Updates enemies and checks if any has gone offscreen
for (int    i=listEnemies.size()-1; i>=0;   i--)    {
Enemies enemy   =   listEnemies.get(i);
enemy.getEnemySprite().setPosition(GameControl.CAMARA_WIDTH,0); // <-- Was experimenting with this code, but doesn't really help now
enemy.mover(-10,0);
if  (enemy.getEnemySprite().getX()<-enemy.getEnemySprite().getWidth())  {
detachChild(enemy.getEnemySprite());        //  Eliminates from scene
listEnemies.remove(enemy);                                                                  //  Eliminates from the list
}
//  Checks collision with center sprite

if  (spriteCenter.collidesWith(enemy.getEnemySprite())) {
detachChild(enemy.getEmemySprite());

}
}
}


Enemies class:

public class Enemies {

private Sprite sprite;

public Enemies(Sprite sprite) {

this.sprite = sprite;
}

public Sprite getEnemySprite() {
return sprite;
}

public void setSprite(Sprite sprite) {
this.sprite = sprite;
}

public void mover(int dx, int dy) {

sprite.setPosition( sprite.getX()+dx, sprite.getY()+dy );
}


}

You only seem to be moving your enemies to the left:

enemy.mover(-10,0);


From what I can see, all of your enemies are being updated with this line of code, so they're all being moved to the left rather than moving towards their desired target. That's why the ones off the right hand side of the screen appear and the others don't.

Try changing your enemy.mover()method to something like this (this isn't particularly good movement code, but hopefully it will help you see where you're going wrong):

public void mover(int targetX, int targetY)
{
int xPos = sprite.getX();
int yPos = sprite.getY();
if (xPos > targetX)
{
xPos--;
}
else if (xPos < targetX)
{
xPos++;
}
if (yPos > targetY)
{
yPos--; //you might need to change this to ++ depending on how the axes are set up
}
else if (yPos < targetY)
{
yPos++; //you might need to change this to -- depending on how the axes are set up
}
sprite.setPosition( xPos, yPos );
}


Then call it by passing in whatever the target is (centre of the screen in your case):

enemy.mover(target.getX(), target.getY());


The above code basically just checks to see if the enemy is either too far left or right of the target (x axis), or too far above or below the target (y axis), and if they are, moves them towards the target.

You'll probably want to change the --'s and ++'s for faster movement. +=10 and -=10 are the equivalents for your current speed.

• Thanks, this works "kinda" fine? It seems that most of my enemies are coming in diagonally and then align to the sprites positions which results in most of them being on the same line. Also, do you know what would be a simple way to set this 4 different spawn zones? thanks a lot btw – OrangeHat Oct 25 '15 at 22:36
• Yeah, that diagonal then aligned movement is what you'd expect to see with that movement code. If you want them to move directly towards the target, you'll need to: 1) get the vector between the enemy and the target, 2) normalise that vector, 3) multiply that vector by the enemy's speed/velocity, then 4) add the result to their position. – KevLoughrey Oct 25 '15 at 23:28
• As for the spawn zones, I guess the simplest way is just to define four positions and when you create an enemy, randomly pass in one of those four positions to it as an argument and create the enemy at that position. If you want to get a bit more advanced, you could define the bounds of the four zones (above, below, left, and right of screen), choose one at random, then generate a random position within that zone. For example, a random pos in the top zone will always have an x position < 0 and a y position > 0 and < screenWidth. It will give a bit more variety to where the enemies come from. – KevLoughrey Oct 25 '15 at 23:32