Move Alien Invaders from Side to Side

edited April 2017 in Questions about Code

Hello all, I'm trying to make a simple space invaders game. For now, the aliens, which are taxis in this case, are moving along down the y-axis without moving horizontally across the x-axis.

This is code that I've written:

boolean move(){
    if(gameMode==EASY){
      y= y+random(0.15, 0.5);
    }else{
      y=y+random(0.3, 0.5);
    }
    for(Taxi taxi: taxis){
      taxi.x= taxi.x+deltaX;
      if(taxi.x>=600){
        deltaX*= -1;
      }

      if(taxi.x<=0){
        deltaX*= -1;
      }
    }
  return (y<=def1.y);
  }

Currently, the taxis are moving from side to side as I wanted. However, right now I'm running with 2 rows, 5 columns of aliens, for some reason, the change of direction is only activated at the 4th column, meaning the 5th column disappears off screen. This is only occurring on the right side too, so taxi.x<=0 is fine, it seems. The width of the game is 400px as indicated in the setup:

void setup(){
  size(400, 700);
  gameMode=SPLASH;
  reset();
}

Thanks!

Tagged:
Sign In or Register to comment.