How would you approach animating characters in Greenfoot when they are made up of separate parts like cut out animation? For example, the Dude Walking scenario in the Greenfoot Scenarios?
import greenfoot.*;
public class Torso extends Body
{
private Leg right, left;
private Arm Aright, Aleft;
private int rot = 1;
private int i = 0;
private boolean spacePressed;
private boolean done = true;
public Torso(Leg q, Leg f, Arm p, Arm k)
{
right = q;
left = f;
Aright = p;
Aleft = k;
}
public void act()
{
keyControl();
limbControl();
jump();
if (getX() >= 559) setLocation( 0, getY());
if (Greenfoot.isKeyDown("space") && done) spacePressed = true;
}
public void keyControl()
{
if (Greenfoot.isKeyDown("right"))
{
setLocation(getX()+1, getY());
walkForward();
}
else if (Greenfoot.isKeyDown("left"))
{
setLocation(getX()-1, getY());
walkBackward();
}
else
{
right.setRotation(0);
left.setRotation(0);
Aright.setRotation(0);
Aleft.setRotation(0);
}
}
public void limbControl()
{
right.setLocation(getX()+27, getY()+84);
left.setLocation(getX()-18, getY()+84);
Aright.setLocation(getX()+29, getY()+18);
Aleft.setLocation(getX()-33, getY()+18);
}
public void walkForward()
{
right.setRotation(right.getRotation()+rot);
left.setRotation(left.getRotation()-rot);
Aright.setRotation(Aright.getRotation()-rot);
Aleft.setRotation(Aleft.getRotation()+rot);
if ((rot < 0 && right.getRotation() > 180 && right.getRotation() <= 340) ||
(rot > 0 && right.getRotation() < 180 && right.getRotation() >= 20)) rot = -rot;
}
public void walkBackward()
{
right.setRotation(right.getRotation()-rot);
left.setRotation(left.getRotation()+rot);
Aright.setRotation(Aright.getRotation()+rot);
Aleft.setRotation(Aleft.getRotation()-rot);
if ((rot > 0 && right.getRotation() > 180 && right.getRotation() <= 340) ||
(rot < 0 && right.getRotation() < 180 && right.getRotation() >= 20)) rot = -rot;
}
public void jump()
{
if (spacePressed)
{
i++;
left.setRotation(90);
right.setRotation(-90);
Aright.setRotation(-90);
Aleft.setRotation(90);
if (i < 50)
{
done = false;
setLocation(getX(), getY()-5);
}
if (i > 50)
{
if (i < 100)
{
done = false;
setLocation(getX(), getY()+5);
}
else if (i == 100)
{
done = true;
spacePressed = false;
i = 0;
left.setRotation(0);
right.setRotation(0);
Aright.setRotation(0);
Aleft.setRotation(0);
}
}
}
}
}