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

2017/2/1

How to set last image of an animation.

nat12 nat12

2017/2/1

#
So I have coded an animation which animates the players movement left, right and jump. However, I had also planned to make him stand still and look left and right depending on which last key was pressed. But I'm getting no where, sometimes it worked only one way, sometimes it worked but when moving right or left it will also animate the standing image and many times it wouldn't work at all. I will show my jumping code first, because part of it is included in the animation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
   int vSpeed = 0;
    int acceleration = 1;
    public void act()
    {
        Move();
        Jump();
        checkFalling();
        checkFloor();
        boom();
    }
    public void Jump ()
    {
        if ((onPlatform() || atWorldFloor()) && "space".equals(Greenfoot.getKey()))
        {
            vSpeed = -16;
            falling();
        }
    }
public void falling()
    {
        setLocation (getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }
    public void checkFalling()
    {
        if(onPlatform() || atWorldFloor() )
        {
 
            vSpeed = 0;
             
        }
        else
        {
            falling();
        }
    }
Animation code (The left animation code is the same but replaced "Right" with "Left" on image name and X-5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public void right()
{
    if(Greenfoot.isKeyDown("right"))
    {
        setLocation(getX()+5, getY()); //Moves the actor right
        if(vSpeed == 0) //Checks if actor is jumping
        {
            if(imageNum <4)
            {
                setImage("Right "+imageNum+".png");
                Greenfoot.delay(1);
                imageNum ++;
            }
            else
            {
                imageNum = 1;
            
        }
        else
        {
            setImage("Right jump.png"); //Else sets right jump image
        }
    }
}
So I had different ways of animating last image, but like I said many times it wouldn't work properly and would even stop my character from jumping. The latest way I tried:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void standing()
{
    int sd = 0;
    if ((Greenfoot.getKey() == "right")) sd++;
    if ((Greenfoot.getKey() == "left")) sd--;
    switch(sd)
    {
        case 1:
        setImage("Right Standing.png");
 
        case -1:
        setImage("Left standing.png");
 
    }
}
This doesn't work, it won't show the Right standing no matter what, and will show Left standing only sometimes when the player is on platform and moved right.
Super_Hippo Super_Hippo

2017/2/1

#
1. You can't use 'Greenfoot.getKey' at more than one position in your code (easily spoken). Try to use 'Greenfoot.isKeyDown' everywhere possible (should be everywhere in your case). 2. Your switch cases are missing a 'break;' statement at their end.
nat12 nat12

2017/2/1

#
Thank you, I changed the jump to iskeydown, however the standing code needs to have two getKey, because it should only change if the last key that WAS pressed is equal to right/left, I can't use iskeydown for this. However, only the left standing animation won't work. After moving to the right and stopping it sets the correct image, but only when the player was moving to the right. (I'm also aware that I didn't include standing in my act method for anyone who thinks that is the problem, I corrected it shortly after posting).
Super_Hippo Super_Hippo

2017/2/1

#
Try this for now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void standing()
{
    int sd = 0;
    String key = Greenfoot.getKey();
    if ("right".equals(key)) sd++;
    if ("left".equals(key)) sd--;
    switch (sd)
    {
        case 1:
        setImage("Right Standing.png");
        break;
         
        case -1:
        setImage("Left standing.png");
        break
    }
}
Note that it is the same as:
1
2
3
String key = Greenfoot.getKey();
if ("right".equals(key)) setImage("Right Standing.png");
else if ("left".equals(key)) setImage("Left Standing.png");
nat12 nat12

2017/2/1

#
Thank you, that worked perfectly. However, I have a problem with my jumping code. I noticed that sometimes whenever his jumping (Image is set to either left or right jumping) he will go straight through the platform. He also sometimes goes into the platform (About 3-4 pixels but doesn't fall through) Is there a way to fix this? I have a code that checks whether his on the platform or not.
1
2
3
4
5
public boolean onPlatform()
 {
     Actor under = getOneObjectAtOffset(0, getImage().getHeight()/2+6, Platform.class);
     return under != null;
 }
And as you can see it is not a permanent value, the value of Height will change depending on the Height of the picture, so in theory is should work will all the images.
Super_Hippo Super_Hippo

2017/2/1

#
The problem is that the actor is moving more than one cell each act cycle and he moves all together without checking in between if he reached the platform. To make it move step by step and check between every step, you could change the falling method to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void falling()
{
    if (vSpeed > 0)
    {
        for (int i=0; i<vSpeed; i++)
        {
            setLocation (getX(), getY()+1);
            if (onPlatform()) return; //exit method
            //or probably
            //if (isTouching(Platform.class)) return;
        }
    }
    else setLocation (getX(), getY()+vSpeed);
     
    vSpeed = vSpeed + acceleration;
}
You need to login to post a reply.