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

2017/1/30

Help with walking animations

crik crik

2017/1/30

#
So, I have been creating this game and I added animations to a "Player" for both left and right movement. When you press the "right" key it moves right and animates right walking and when you press the "left" key it moves left and animates left walking. However, when either the left or right key is pressed, even once, as soon as it is pressed and you let go of it does not return to the original standing PNG that the "Player" class is set to and I have no idea how to fix it so that when the key is pressed and let go, it returns to either a left or right standing png for whichever direction the class is facing. (I have a total of 14 PNGs for the animation, 6 right walking PNGs, 6 left walking PNGs, and then a left and right standing PNG). If anyone can help me with this code it would be very much appreciated, the movement works perfectly, just a small little animation problem that makes the animation looks weird when the player is not moving. I will list any necessary code down below.
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
 
/**
 * Write a description of class Player here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    private int vSpeed = 0;
    private int acceleration = 3;
    private boolean jumping;
    private int jumpStrength = 30;
    private int speed = 5;
    private int punchDelay = 0;
    private int delay = 0;
 
    private GreenfootImage run1l = new GreenfootImage("frame-1left.png");
    private GreenfootImage run2l = new GreenfootImage("frame-2left.png");
    private GreenfootImage run3l = new GreenfootImage("frame-3left.png");
    private GreenfootImage run4l = new GreenfootImage("frame-4left.png");
    private GreenfootImage run5l = new GreenfootImage("frame-5left.png");
    private GreenfootImage run6l = new GreenfootImage("frame-6left.png");
    private GreenfootImage run7l = new GreenfootImage("standing 1left.png");
 
    private GreenfootImage run1r = new GreenfootImage("frame-1.png");
    private GreenfootImage run2r = new GreenfootImage("frame-2.png");
    private GreenfootImage run3r = new GreenfootImage("frame-3.png");
    private GreenfootImage run4r = new GreenfootImage("frame-4.png");
    private GreenfootImage run5r = new GreenfootImage("frame-5.png");
    private GreenfootImage run6r = new GreenfootImage("frame-6.png");
    private GreenfootImage run7r = new GreenfootImage("standing 1.png");
 
    private int frame = 0;
    private int animationCounter = 0;
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        checkFall();
        checkKey();
        platformAbove();
        checkRightWalls();
        checkLeftWalls();
        counter.act();
        if(counter.getValue() <= 0)
        {
            MyWorld world = (MyWorld) getWorld();
            world.gameOver2();
        }
        animationCounter++;
    
 
    public void checkKey()
    {
        if(Greenfoot.isKeyDown("w") && jumping == false)
        {
            jump();
            Greenfoot.playSound("jump.wav");
        }
        if(Greenfoot.isKeyDown("a"))
        {
            moveLeft();
        }
        if(Greenfoot.isKeyDown("d"))
        {
            moveRight();
        }
        if(Greenfoot.isKeyDown("s"))
        {
            punch();
        }
    }
 
    public void moveLeft()
    {
        setLocation(getX() - speed, getY());
        if(animationCounter % 4 == 0)
        {
            animateLeft();
        }
    }
 
    public void animateLeft()
    {
        if(frame == 0)
        {
            setImage(run1l);
        }
        else if(frame == 1)
        {
            setImage(run2l);
        }
        else if(frame == 2)
        {
            setImage(run3l);
        }
        else if(frame == 3)
        {
            setImage(run4l);
        }
        else if(frame == 4)
        {
            setImage(run5l);
        }
        else if(frame == 5)
        {
            setImage(run6l);
            frame = 0;
        }
        frame++;
        return;
    }
 
    public void moveRight()
    {
        setLocation(getX() + speed, getY());
 
        if(animationCounter % 4 == 0)
        {
            animateRight();
        }
    }
 
    public void animateRight()
    {
        if(frame == 0)
        {
            setImage(run1r);
        }
        else if(frame == 1)
        {
            setImage(run2r);
        }
        else if(frame == 2)
        {
            setImage(run3r);
        }
        else if(frame == 3)
        {
            setImage(run4r);
        }
        else if(frame == 4)
        {
            setImage(run5r);
        }
        else if(frame == 5)
        {
            setImage(run6r);
            frame = 0;
        }
        frame++;
        return;
    }
danpost danpost

2017/1/30

#
You just need a field to retain which direction the player was last moving to determine which image to use when still. You would still need a way to determine if the actor is still or not. One way to side-step this issue is by setting the still image before checking the 'a' and 'd' keys. There is another thing that could be annoying -- like how when holding both the 'a' and 'd' keys down simultaneously, the player will always be animating right without actually moving. To avoid that, you can check the state of both keys before moving either way. I usually do this by setting a new direction int field to zero and letting the downed keys adjust the value of the field:
1
2
3
int dir = 0;
if (Greenfoot.isKeyDown("a")) dir--;
if (Greenfoot.isKeyDown("d")) dir++;
The end result of 'dir' will be zero if neither or both keys were down and you can set the appropriate still image using the last facing field. If its value is not zero, then call 'moveRight' if positive and 'moveLeft' if negative. Set the last facing direction field within these 'moveRight' and 'moveLeft' methods. That should make everything look a lot better.
You need to login to post a reply.