This site requires JavaScript, please enable it in your browser!
Greenfoot back
meazalplaq
meazalplaq wrote ...

2014/12/2

Animating Seperate Character Parts

meazalplaq meazalplaq

2014/12/2

#
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?
danpost danpost

2014/12/2

#
I adjusted the Torso class to the following:
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);
                }
            }
        }
    }
}
I did consolidate a little in that, instead of a 'leftrot' and 'rightrot', I used one field, 'rot' for both. The fix for the legs and arms are on lines 64 and 74, where the conditions were made more precise.
danpost danpost

2014/12/2

#
I do not know if I would have approached it quite the same way. Although, all in all, this is not bad.
meazalplaq meazalplaq

2014/12/2

#
Thank you!
danpost danpost

2014/12/3

#
I gave it a try. Came up with something similar using 107 lines of code in the class which included about 25 lines for creating the images of the body parts. I do not think it turned out badly. The character looks stiff -- almost robot-like; until it jumps.
danpost danpost

2014/12/6

#
I felt that I might as well upload what I came up with (with source) so people can see how I accomplished it. I will post a link after it is up. I did add a little more code for imaging and documented it a bit. Here is the link to my Animated Character.
meazalplaq meazalplaq

2014/12/7

#
Fantastic work!
You need to login to post a reply.