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

2015/2/17

Jump Animation

EduandErnst EduandErnst

2015/2/17

#
Hi there, I'm trying to make my actor jump and I'm having difficulties to animate this. Does anyone know how to setup an animation so that it starts when I leave (i.e. the ground) and stops as soon as I reach the ground again. I have 5 different pictures stored for my jump animation and they should all be shown in this movement. What I got now only shows 2 pics of the whole animation so I probably would need to speed it a little up but how?
Super_Hippo Super_Hippo

2015/2/17

#
If you already have some code there, you could show it. It would be easier to work with it then. Use the code tags shown below. In general, you will have for example a boolean indicating if you are in the air and change it when you jump and when you reach the ground again. If this boolean is true, then the jump animation is shown. If you have more then two animations, it is easier to use an int instead of a boolean for it. The animation right now probably has a timer and when a specified value is reached, the next image is shown. There you can change the speed of the animation.
EduandErnst EduandErnst

2015/2/23

#
Yes, right now I have a simple code which counts up but I wouldn't know how to speed up the counting. Of course I could say jumpFrameR += 2; instead of jumpFrameR++; but this would just skip lots of pics....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void animateStraightJumpingR(){
         
        if (jumpFrameR == 1){
            setImage (straightjumpr1);
        } else if (jumpFrameR == 2){
            setImage (straightjumpr2);
        } else if (jumpFrameR == 3){
            setImage (straightjumpr3);
        } else if (jumpFrameR == 4){
            setImage (straightjumpr4);
        } else if (jumpFrameR == 5){
            setImage (straightjumpr5);
            frame = 1;
             
        }
        jumpFrameR++;
    }
on my right/left movement I used a switch-case-default combination like that
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
else if (Greenfoot.isKeyDown("left") )
        {
 
            dx -= 1;
            frame++;
            standRight = false;
            switch (frame){
                case 1: setImage(boyWalkleft1); break;
                case 5: setImage(boyWalkleft2); break;
                case 9: setImage(boyWalkleft3); break;
                case 14: setImage(boyWalkleft4); break;
                case 19: setImage(boyWalkleft5); break;
                case 24: setImage(boyWalkleft6); break;
                case 29: setImage(boyWalkleft7); break;
                case 34: setImage(boyWalkleft8); break;
                case 39: setImage(boyWalkleft9); break;
                case 44: setImage(boyWalkleft10); break;
                case 49: setImage(boyWalkleft11); break;
                case 54: setImage(boyWalkleft12); break;
                case 59: setImage(boyWalkleft13); break;
                case 64: setImage(boyWalkleft14); break;
                case 69: setImage(boyWalkleft15); break;
                case 74: setImage(boyWalkleft16); break;
                case 79: setImage(boyWalkleft17); break;
                case 84: setImage(boyWalkleft18); break;
                case 89: setImage(boyWalkleft19); break;
                case 94: setImage(boyWalkleft20); break;
                case 99: setImage(boyWalkleft21); break;
                case 104: setImage(boyWalkleft22); break;
 
                case 108: frame = 0; break;
 
            }
I tried it with my jumping animation as well but I was not able to speed it up it only shows like two pictures every time I jump.
Super_Hippo Super_Hippo

2015/2/23

#
If you are calling the 'animateStraightJumpingR' method every act-cycle when you are jumping, you should see all images there. From where are you calling this method? I don't know if it is like a cycled animation, but the first image will be skipped after the first time because you set 'frame' to 1 in line 13, which is 2 after line 16 then. By the way, if you would organize your images in arrays, you wouldn't have to create such big if-else trees or switch-statements.
danpost danpost

2015/2/23

#
Your images will not change once the "left" key is released with your left-right movement. You need a delay between image changes in the animation for jumping.
EduandErnst EduandErnst

2015/2/23

#
Sorry guys neither my English is perfect nor my knowledge about java, I'm German and quite a beginner in java. ^^ @Super_Hippo: Right now I call the jump animation every time I press the "up" key. Which is pretty much an act-cycle isn't it? @Danpost I heard about the delay option, but I don't really know how to use it. Is there an explanation somewhere on the forum? Thanks btw. for the great support :)
danpost danpost

2015/2/23

#
Just to get the idea, you could change the values on lines 3, 5, 7, 9 and 11 of the 'animateStraightJumpingR' method from '1', '2', '3', '4' and '5' to '10', '20', '30', '40' and '50'. I believe that line 13 should set 'jumpFrameR' to zero for your images to cycle.
EduandErnst EduandErnst

2015/2/24

#
Ok I've changed my code as you told me but it didn't work. I'll post you the whole jumping code so you can see where the issue might be. Maybe I've written something wrong
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
public class boy extends Actor
{
 
    private boolean jumping;
    private int jumpStrength = 30;
    private int gravity;
    private int acceleration = 2;
     
    public void act()
    {
        checkKeys();
        checkForGround();
        checkFall();
        onGround();
    }   
 
    public void checkKeys(){
        if (Greenfoot.isKeyDown("up") && jumping == false) {
            jump();
        }
    }
 
    public void jump(){
        animateStraightJumpingR();
        gravity = gravity - jumpStrength;
        jumping = true;
        fall();
    }
 
    public boolean checkForGround(){
        int spriteHeight = getImage().getHeight();
        int lookForGround = (int) (spriteHeight/2) +5;
 
        Actor ground = getOneObjectAtOffset(0, lookForGround, Ground.class);
 
        if(ground == null) {
            jumping = true;
            return false;
        } else {
            moveToGround(ground);
            return true;
        }
    }
     
    public boolean onGround(){
        int spriteHeight = getImage().getHeight();
        int lookForGround = (int) (spriteHeight/2) +5;
 
        Actor ground = getOneObjectAtOffset(0, lookForGround, Ground.class);
 
        if(ground == null) {
            jumping = true;
            return false;
        } else {
            moveToGround(ground);
            return true;
        }
 
    }
     
    public void moveToGround(Actor ground){
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
 
        setLocation(getX(), newY);
        jumping = false;
    }
 
    public void checkFall(){
        if(onGround()){
            gravity = 0;
        } else {
            fall();
        }
    }
     
    public void fall(){
        setLocation(getX(), getY() + gravity);
        if (gravity <= 10){
            gravity = gravity + acceleration;
 
        }
        jumping = true;
    }
     
    public void animateStraightJumpingR(){
 
        if (jumpFrameR == 10){
            setImage (straightjumpr1);
        } else if (jumpFrameR == 20){
            setImage (straightjumpr2);
        } else if (jumpFrameR == 30){
            setImage (straightjumpr3);
        } else if (jumpFrameR == 40){
            setImage (straightjumpr4);
        } else if (jumpFrameR == 50){
            setImage (straightjumpr5);
            frame = 0;
 
        }
        jumpFrameR += 10;
    }
 
}
danpost danpost

2015/2/24

#
EduandErnst wrote...
Ok I've changed my code as you told me but it didn't work.
I did not say anything about changing the incremental value on 'jumpFrameR' (line 101); however, I did say something about changing what was being zeroed on line 98 (it should not be 'frame', but 'jumpFrameR' that is zeroed). Also, your jump animation is only executed on the condition that the "up" key is pressed. To be more specific, if your actor is (already) jumping and the "up" key is released, the animation will stop regardless of where in the process of jumping it is. This is due to the 'animateStraightJumpingR' method being called from the 'jump' method, which in turn is being called from the 'checkKeys' method. 'jump' is called only when the "up" key is found down, so the animation will not execute unless the key is down.
Super_Hippo Super_Hippo

2015/2/24

#
'animateStraightJumpingR' is only called once when you jump off the ground. Instead (so remove it in line 24), you could call it every act cycle when 'jumping' is true for example at the end of the act method:
1
if (jumping) animateStraightJumpingR;
In the method, you changed 1, 2, 3, 4 and 5 to ten times of those values, but you also increase 'jumpFrameR' ten times faster, so this doesn't change anything. So line 101 should still be 'jumpFrameR++;'.
danpost danpost

2015/2/24

#
Super_Hippo is correct in that the animation is only called at the start of a jump. I did not take into account the condition that 'jumping == false' (line 18) which prevents the animation from being executed beyond the instant the jump is initiated.
EduandErnst EduandErnst

2015/2/27

#
Yes, that works just as I want it. Thanks again
You need to login to post a reply.