I modified my Cube class (made the 'left/right' movement more precise when intersecting walls:
import greenfoot.*;
import java.awt.Color;
public class Cube extends Actor
{
private int gravity;
public Cube()
{
GreenfootImage image = new GreenfootImage(49, 49);
image.setColor(Color.gray);
image.fill();
setImage(image);
}
public void act()
{
// gravity and jump
gravity += 2; // add gravity
turn(90); // face 'down'
move(gravity); // fall faster (or rise slower)
boolean hitWall = false;
while (getOneIntersectingObject(Wall.class) != null)
{ // move off any intersecting wall
hitWall = true;
move(-(int)Math.signum(gravity));
}
if (hitWall)
{ // add buffer space and stop fall (or rise)
move(-(int)Math.signum(gravity));
gravity = 0;
// jump on command
if (Greenfoot.isKeyDown("w") || Greenfoot.isKeyDown("up")) gravity -= 30;
}
turn(-90); // re-face 'forward'
// left and right
int d = 0; // to hold direction input by user
if (Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("left")) d--;
if (Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("right")) d++;
if (d != 0)
{
move(4*d); // move in given direction
hitWall = false;
while (getOneIntersectingObject(Wall.class) != null)
{ // move off any intersecting wall
hitWall = true;
move(-d);
}
if (hitWall)
{ // turn and add buffer space from side wall
turn(-90*d); // new gravity direction
move(d); // add buffer from old ground wall
gravity = 0;
}
}
}
}

