I´m trying to get my actor to move on a line of blocks, but sometimes it skips some of them. Thanks for any help.
My code:
public class Ghost extends Actor
{
String lastMove = null;
int x;
int y;
public void act()
{
move();
}
public void move()
{
if (getOneObjectAtOffset(0,1,Block.class)!=null)
{
if(lastMove != "up")
{
moveDown();
}
}
if (getOneObjectAtOffset(0,-1,Block.class)!=null)
{
if (lastMove != "down")
{
moveUp();
}
}
if (getOneObjectAtOffset(-1,0,Block.class)!=null)
{
if(lastMove != "right")
{
moveLeft();
}
}
if (getOneObjectAtOffset(1,0,Block.class)!=null)
{
if(lastMove != "left")
{
moveRight();
}
}
}
public void moveDown()
{
getCoordiantes();
setLocation(x,y+1);
lastMove = "down";
}
public void moveUp()
{
getCoordiantes();
setLocation(x,y-1);
lastMove = "up";
}
public void moveLeft()
{
getCoordiantes();
setLocation(x-1,y);
lastMove = "left";
}
public void moveRight()
{
getCoordiantes();
setLocation(x+1,y);
lastMove = "right";
}
public void getCoordiantes()
{
x = getX();
y = getY();
}
}
