Good luck!
public class GroundMover extends Actor
{
public void detectCollision(int moveSpeed, int xMomentum, int yMomentum)
{
if(getOneIntersectingObject(nonPassable.class) != null)
{
if(xMomentum > 0) //moving right
{
setLocation((getX() - moveSpeed), getY());
}
if(xMomentum < 0) //moving left
{
setLocation((getX() + moveSpeed), getY());
}
if(yMomentum > 0) //moving down
{
setLocation(getX(), (getY() - moveSpeed));
}
if(yMomentum < 0) //moving up
{
setLocation(getX(), (getY() + moveSpeed));
}
}
}
}
public void act()
{
move(4);
}
public void move(int moveAmt)
{
// determine direction by keypress checking
int dx = 0, dy = 0;
if (Greenfoot.isKeyDown("d"))
{dx += 1;}
if (Greenfoot.isKeyDown("a"))
{dx -= 1;}
if (Greenfoot.isKeyDown("s"))
{dy += 1;}
if (Greenfoot.isKeyDown("w"))
{dy -= 1;}
// check for wall on each step of move in both vertical and horizontal directions
for (int i = 0; i < moveAmt; i++)
{
setLocation(getX() + dx, getY());
if (getOneIntersectingObject(Wall.class) != null) setLocation(getX() - dx, getY());
setLocation(getX(), getY() + dy);
if (getOneIntersectingObject(Wall.class) != null) setLocation(getX(), getY() - dy);
}
}