I have a question about getting an actor to traverse the entire grid, going from one square to the next. Here is the code for my actor class. I can get it to traverse the grid until the second row when it goes up instead of down.
Any help is much appreciated.
David
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.List;
import java.util.ArrayList;
public class Pacman extends Actor
{
public static int cellSize;
private GreenfootImage pacmanRight;
private GreenfootImage pacmanLeft;
private GreenfootImage pacmanEat;
private GreenfootImage image;
private int foodEaten;
private boolean stopped;
public Pacman()
{
pacmanRight = PacmanWorld.loadImage("pacman1.gif");
pacmanLeft = new GreenfootImage(pacmanRight);
pacmanLeft.rotate(0);
pacmanEat = PacmanWorld.loadImage("pacman3.gif");
setImage(pacmanRight);
cellSize = PacmanWorld.CELL_SIZE;
foodEaten = 0;
}
public void act()
{
if(foundFood())
{
eatFood();
}
else if(!isAtEdge())
{
move(1);
}
else
{
turn(90);
move(1);
if(!isAtEdge()){
turn(90);
}
}
}
public boolean isAtEdge()
{
int rotation;
boolean result;
rotation = getRotation();
result = false;
if (rotation == 0)
{
result = (getX() >= (getWorld().getWidth()-1));
}
else if (rotation == 90)
{
result = (getY() >= (getWorld().getHeight()-1));
}
else if (rotation == 180)
{
result = (getX() <= 0);
}
else if (rotation == 270)
{
result = (getY() <= 0);
}
return result;
}
public boolean foundFood()
{
Actor food;
boolean result;
food = getOneObjectAtOffset(0, 0, Food.class);
if(food != null)
{
result = true;
}
else
{
result = false;
}
return result;
}
public void eatFood()
{
Actor food;
food = getOneObjectAtOffset(0, 0, Food.class);
if(food != null)
{
getWorld().removeObject(food);
foodEaten++;
}
}
public int getFoodEaten()
{
return foodEaten;
}
public boolean isStopped()
{
return stopped;
}
public GreenfootImage loadImage(String string)
{
GreenfootImage image;
image = new GreenfootImage(string);
if ((image.getWidth() > cellSize) || (image.getHeight() > cellSize))
{
image.scale(cellSize, cellSize);
}
return image;
}
}
