Here is my actor code
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class turtle here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Ash extends Actor
{
private GreenfootImage image1;
private GreenfootImage image2;
private GreenfootImage image3;
private GreenfootImage image4;
private GreenfootImage image5;
private GreenfootImage image6;
private GreenfootImage image7;
private GreenfootImage image8;
public Ash()
{
image1 = new GreenfootImage("down1.png");
image2 = new GreenfootImage("down2.png");
image3 = new GreenfootImage("left1.png");
image4 = new GreenfootImage("left2.png");
image5 = new GreenfootImage("right1.png");
image6 = new GreenfootImage("right2.png");
image7 = new GreenfootImage("up1.png");
image8 = new GreenfootImage("up2.png");
}
/**
* Act - do whatever the turtle wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move();
checkCollision();
}
public void move()
{
if( Greenfoot.isKeyDown("down"))
{
moveDown();
}
if( Greenfoot.isKeyDown("up"))
{
moveUp();
}
if( Greenfoot.isKeyDown("left"))
{
moveLeft();
}
if( Greenfoot.isKeyDown("right"))
{
moveRight();
}
}
public void checkCollision()
{
Actor a = getOneIntersectingObject( Wall.class );
if ( a != null )
{
if( Greenfoot.isKeyDown("down"))
{
wallDown();
}
if( Greenfoot.isKeyDown("up"))
{
wallUp();
}
if( Greenfoot.isKeyDown("left"))
{
wallLeft();
}
if( Greenfoot.isKeyDown("right"))
{
wallRight();
}
}
}
public void moveDown()
{
int x = (getX());
int y = (getY() + 1);
setLocation(x, y);
if (getImage() == image1)
{
setImage(image2);
}
else
{
setImage(image1);
}
}
public void moveUp()
{
int x = (getX());
int y = (getY() - 1);
setLocation(x, y);
if (getImage() == image8)
{
setImage(image7);
}
else
{
setImage(image8);
}
}
public void moveLeft()
{
int x = (getX() - 1);
int y = (getY());
setLocation(x, y);
if (getImage() == image4)
{
setImage(image3);
}
else
{
setImage(image4);
}
}
public void moveRight()
{
int x = (getX() + 1);
int y = (getY());
setLocation(x, y);
if (getImage() == image6)
{
setImage(image5);
}
else
{
setImage(image6);
}
}
public void wallDown()
{
int x = (getX());
int y = (getY() - 1);
setLocation(x, y);
}
public void wallUp()
{
int x = (getX());
int y = (getY() + 1);
setLocation(x, y);
}
public void wallLeft()
{
int x = (getX() + 1);
int y = (getY());
setLocation(x, y);
}
public void wallRight()
{
int x = (getX() - 1);
int y = (getY());
setLocation(x, y);
}
}