I am working on a space invaders style game, and I am having trouble getting the enemies to move correctly. I am trying to get them to move back and forth across the screen, but for some reason the enemies just go to the right side and stay there. this is the code i currently have:
i also tried this (FYI world size is 800x600):
and the whole class (currently) is:
if (movementDirection == "right")
{
setLocation(getX() + 1, getY());
}else if (movementDirection == "left")
{
setLocation(getX() - 1, getY());
}
if (isAtEdge())
{
if(movementDirection == "right") {movementDirection = "left";}
if (movementDirection == "left") {movementDirection = "right";}
}if(movementDirection == "right" && getX() < 800)
{
setLocation(getX() + 1, getY());
} else if(movementDirection == "right" && getX() >= 800)
{
movementDirection = "left";
} else if (movementDirection == "left" && getX() > 0)
{
setLocation(getX() - 1, getY());
}else if (movementDirection == "left" && getX() <= 0)
{
movementDirection = "right";
}else
{
move(-100);
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
public class enemyShip extends Actor
{
public static int health = 3;
public String movementDirection = "right";
public void act()
{
/*
if(movementDirection == "right" && getX() < 800)
{
setLocation(getX() + 1, getY());
} else if(movementDirection == "right" && getX() >= 800)
{
movementDirection = "left";
} else if (movementDirection == "left" && getX() > 0)
{
setLocation(getX() - 1, getY());
}else if (movementDirection == "left" && getX() <= 0)
{
movementDirection = "right";
}else
{
move(-100);
}*/
if (movementDirection == "right")
{
setLocation(getX() + 1, getY());
}else if (movementDirection == "left")
{
setLocation(getX() - 1, getY());
}
if (isAtEdge())
{
if(movementDirection == "right") {movementDirection = "left";}
if (movementDirection == "left") {movementDirection = "right";}
}
if(isTouching(spaceShip.class))
{
spaceShip.ded = true;
getWorld().getBackground().drawImage(new GreenfootImage("GAME OVER", 50, Color.blue, Color.black), 350, 250);
}
if (health <= 0)
{
getWorld().removeObject(this);
}
}
public static void hit()
{
health--;
}
}
