I'm currently trying to create a space invaders type of game. I currently have one row of 4 alien invaders (I plan on adding more rows later). What I want the program to do is to keep moving right and once one alien of the touches the edge of the world, the entire row moves down and continues in the opposite direction. However, I can only make each individual alien touch the edge of the world, go down and reverse which in turn makes the aliens look like they are flying in an S pattern all the way down to the bottom of the screen. Here is the code for the actor class that controls the aliens:
Any help would be appreciated as I am still learning the ropes with Greenfoot. Thank you in advance!
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Alien here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Alien extends Actor
{
double HoverSpeed = 2.5;
double Base = 0;
double Height = 20;
double Alt;
/**
* Act - do whatever the Alien wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
AssignLocation();
MoveShips();
Destroyed();
}
public void AssignLocation(){
setLocation((int)(getX() + Base), (int)(getY() + Alt) );
}
public void MoveShips() {
Base = HoverSpeed;
Alt = 0;
if (isAtEdge()){
HoverSpeed = HoverSpeed*-1;
Base = HoverSpeed;
Alt = Height;
}
}
public void Destroyed() {
if (isTouching(Missile.class)) {
getWorld().removeObject(this);
}
}
}