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

2019/5/12

Falling Animation

AniSusi AniSusi

2019/5/12

#
So, I'm programming a jump& run type of game and my character can jump on platforms and fall off them. I did some animation with left & right movement and also jumping. However, I have no Idea how to set the image to a falling.png if the character falls off a platform. Also it would be neat if he changes the image after comming back down from a jump. I'd really appreciate some help! heres the code I have so far :
public class player extends Actor
{
    private final int GRAVITY=1;
    private final int SPEED=5;
    private int speed1;
    private GreenfootImage stand1 = new GreenfootImage("stand1.png");
    private GreenfootImage stand2 = new GreenfootImage("stand2.png");
    private GreenfootImage run1 = new GreenfootImage("run1.png");
    private GreenfootImage run2 = new GreenfootImage("run2.png");
    private GreenfootImage run3 = new GreenfootImage("run3.png");
    private GreenfootImage run4 = new GreenfootImage("run4.png");
    private GreenfootImage jump1 = new GreenfootImage("jump1.png");
    private GreenfootImage jump2 = new GreenfootImage("jump2.png");
    private GreenfootImage fall1 = new GreenfootImage("fall1.png");
    private GreenfootImage fall2 = new GreenfootImage("fall2.png");
    private int frame=1;
    private int counter=0;
    private int blick;

public player()
    {
        speed1=0;
    }
    public void act()
    {
        fall();
        jump();
        move();
        stand();
        counter++;
    }
    public void fall()
    {
        setLocation(getX(),getY()+speed1);
        if(isOnSolidGround())
        {
            speed1=0;
            while(isOnSolidGround())
            {
                setLocation(getX(),getY()-1);
            }
            setLocation(getX(),getY()+1);
        }
        else if(speed1<0 && bumpHead())
        {
            speed1=0;
            while(bumpHead())
            {
                setLocation(getX(),getY()+1);
                if(blick==1)
                {
                    setImage(fall1);
                }
                if(blick==0)
                {
                    setImage(fall2);
                }
            }
        }
        else
        {
            speed1+=GRAVITY;
        }
    }
    public void jump()
    {
        if(Greenfoot.isKeyDown("w") && isOnSolidGround())
        {
            speed1=-15;
            if(blick==1)
            {
                setImage(jump1);
            }
            if(blick==0)
            {
                setImage(jump2);
            }
        }
    }
    public void move()
    {
        int y=getY();
        int x=getX();
        if(Greenfoot.isKeyDown("a") && canMoveLeft())
        {
            x-=SPEED;
            if(counter % 10 == 0 && isOnSolidGround())
            {
                animateLeft();
            }
            blick=0;
        }
        if(Greenfoot.isKeyDown("d") && canMoveRight())
        {
            x+=SPEED;
            if(counter % 10 == 0 && isOnSolidGround())
            {
                animateRight();
            }
            blick=1;
        }
        setLocation(x,y);
    }
    public void animateRight()
    {
        if(frame == 1)
        {
            setImage(run1);
        }
        else if(frame == 2)
        {
            setImage(run2);
            frame = 1;
            return;
        }
        frame++;
    }
    public void animateLeft()
    {
        if(frame == 1)
        {
            setImage(run3);
        }
        else if(frame == 2)
        {
            setImage(run4);
            frame = 1;
            return;
        }
        frame++;
    }
    public void stand()
    {
        if(!Greenfoot.isKeyDown("a") && !Greenfoot.isKeyDown("d") && !Greenfoot.isKeyDown("w") && isOnSolidGround())
        {
            if(blick==1)
            {
                setImage(stand1);
            }
            if(blick==0)
            {
                setImage(stand2);
            }
        }
    }
    public boolean isOnSolidGround()
    {
        boolean isOnGround=false;
        if(getY()>getWorld().getHeight()-2)
        {
            isOnGround=true;
        }
        int imageWith=getImage().getWidth();
        int imageHeight=getImage().getHeight();
        if(getOneObjectAtOffset(imageWith/-2,imageHeight/2,platform.class)!=null ||
           getOneObjectAtOffset(imageWith/ 2,imageHeight/2,platform.class)!=null)
        {
            isOnGround=true;
        }
        return isOnGround;
    }
    public boolean bumpHead()
    {
        boolean bumpedHead=false;
        int imageWith=getImage().getWidth();
        int imageHeight=getImage().getHeight();
        if(getOneObjectAtOffset(imageWith/-2,imageHeight/-2,platform.class)!=null ||
           getOneObjectAtOffset(imageWith/ 2,imageHeight/-2,platform.class)!=null)
        {
            bumpedHead=true;
        }
        return bumpedHead;
    }
    public boolean canMoveLeft()
    {
        boolean canMoveLeft=true;
        int imageWith=getImage().getWidth();
        int imageHeight=getImage().getHeight();
        if(getOneObjectAtOffset(imageWith/-2-SPEED,imageHeight/-2  ,platform.class)!=null ||
           getOneObjectAtOffset(imageWith/-2-SPEED,imageHeight/ 2-1,platform.class)!=null)
        {
            canMoveLeft=false;
        }
        return canMoveLeft;
    }
    public boolean canMoveRight()
    {
        boolean canMoveRight=true;
        int imageWith=getImage().getWidth();
        int imageHeight=getImage().getHeight();
        if(getOneObjectAtOffset(imageWith/ 2+SPEED,imageHeight/-2  ,platform.class)!=null ||
           getOneObjectAtOffset(imageWith/ 2+SPEED,imageHeight/ 2-1,platform.class)!=null)
        {
            canMoveRight=false;
        }
        return canMoveRight;
    }
}
danpost danpost

2019/5/13

#
It might be easiest if you placed each animation image set in an array; then make an array of animation sets:
// the individual animation sets
private GreenfootImage[] standing = new GreenfootImage[] { stand1, stand2 };
private GreenfootImage[] running = new GreenfootImage[] { run1, run2, run3, run4 };
private GreenfootImage[] jumping = new GreenfootImage[] { jump1, jump2 };
private GreenfootImage[] falling = new GreenfootImage[] { fall1, fall2 };

// all animation sets
private GreenfootImage[][] anims = new GreenfootImage[][] { standing, running, jumping, falling };
You could actually name the sets:
private static final int STAND = 0, RUN = 1, JUMP = 2, FALL = 3;
and add a field for which is the currently active animation:
private int animating = STAND;
You should probably have others for the frame number and delay timer as well:
private int frame, animTimer;
This should be zeroed any time the current animation set changes. This change should be done only after all movement has been done and you have determined which set should be used AND this set is NOT the current set being used. The steps involved could be something like this: * set up a local int field for potential image set to use (set standing) * move horizontally; if moves, assign running; * move vertically; if not on ground, assign jump or fall (depending on sign of vertical speed value); * if potential is current image set, run timer/frame; else change image set and zero frame number and timer.
You need to login to post a reply.