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

2014/8/28

Issue with enemy knockback

K_wow K_wow

2014/8/28

#
I am experiencing a glitch with my zombie fighter game where the zombies will be affected by the player's knockback. Here's my player code:
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
 
/**
 * Write a description of class Player here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    public static final int DEFAULT_SPEED = 2;
    public int walkProgress = 10;
    public boolean isWalking = false;
    public int speed = DEFAULT_SPEED;
    public int swordTimer = 0;
    public boolean backSwing = false;
    public int spinProgress = 0;
    public int spinTimer = 0;
    public int gunTimer = 0;
    public int knifeTimer = 0;
    public int joustTimer = 0;
    public int lives = 10;
    public int invincibilityTimer = 0;
    public int effectTimer = 0;
    public int knockbackLeft = 0;
    public int knockbackSpeed = 0;
    public int combo = 0;
    public int comboTimer = 0;
    public int currentWeapon = 1;
    public int currentItem = 1;
 
    public String leftKey = "";
    public String rightKey = "";
    public String upKey = "";
    public String downKey = "";
    public String weaponKey = "";
    public String specialKey = "";
    public String itemKey = "";
 
    public Color defaultColourLight = new Color(140, 157, 255);
    public Color defaultColour = new Color(0, 38, 255);
    public Color defaultColourDark = new Color(0, 19, 130);
    public Color nextColourLight = new Color(255, 140, 140);
    public Color nextColour = new Color(255, 0, 0);
    public Color nextColourDark = new Color(127, 0, 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()
    {
        Board board = (Board)getWorld();
        if (board.playing)
        {
            if (lives > 0)
            {
                if (knockbackLeft <= 0)
                {
                    moveAround();
                    knockbackSpeed = 0;
                }
                else
                {
                    if (invincibilityTimer > 0)
                    {
                        setImage("player_blue_walk" + ((walkProgress / 10) - 1) + "_hurt.png");
                    }
                    turn(180);
                    move(knockbackSpeed);
                    knockbackLeft -= knockbackSpeed;
                    turn(-180);
                }
                useWeapons();
 
                // setColour(defaultColour, defaultColourDark, nextColour, nextColourDark);
            }
            else
            {
            }
            if (lives > 0)
            {
                if (lives > 10)
                {
                    lives = 10;
                }
                if (invincibilityTimer > 0)
                {
                    invincibilityTimer--;
                }
                if (effectTimer > 0)
                {
                    effectTimer--;
                }
                if (comboTimer > 0)
                {
                    comboTimer--;
                }
                else
                {
                    combo = 0;
                }
                updateArms();
            }
            else
            {
                if (currentItem == 1)
                {
                    getWorld().addObject(new Medpack(2, getRotation(), 30, false), getX(), getY());
                }
                else if (currentItem == 2)
                {
                    getWorld().addObject(new Bomb(2, getRotation(), 50, false), getX(), getY());
                }
                die();
            }
        }
    }   
 
    public void moveAround()
    {
        if (joustTimer > 40)
        {
            speed *= 3;
            if (effectTimer <= 0)
            {
                getWorld().addObject(new Placeholder(getRotation(), 1, getImage(), 150), getX(), getY());
                getWorld().addObject(new Placeholder(getRotation(), 1, getArms().getImage(), 150), getX(), getY());
                effectTimer = 3;
            }
        }
        if (Greenfoot.isKeyDown(leftKey))
        {
            setRotation(180);
            move(speed);
        }
        if (Greenfoot.isKeyDown(rightKey))
        {
            setRotation(0);
            move(speed);
        }
        if (Greenfoot.isKeyDown(upKey))
        {
            setRotation(270);
            move(speed);
        }
        if (Greenfoot.isKeyDown(downKey))
        {
            setRotation(90);
            move(speed);
        }
        if (Greenfoot.isKeyDown(downKey) && Greenfoot.isKeyDown(rightKey))
        {
            setRotation(45);
        }
        if (Greenfoot.isKeyDown(downKey) && Greenfoot.isKeyDown(leftKey))
        {
            setRotation(135);
        }
        if (Greenfoot.isKeyDown(upKey) && Greenfoot.isKeyDown(leftKey))
        {
            setRotation(225);
        }
        if (Greenfoot.isKeyDown(upKey) && Greenfoot.isKeyDown(rightKey))
        {
            setRotation(315);
        }
        if (!Greenfoot.isKeyDown(leftKey) && !Greenfoot.isKeyDown(rightKey) && !Greenfoot.isKeyDown(upKey) && !Greenfoot.isKeyDown(downKey))
        {
            isWalking = false;
        }
        else
        {
            isWalking = true;
        }
        speed = DEFAULT_SPEED;
 
        if (!isWalking)
        {
            walkProgress = 20;
            if (invincibilityTimer > 0)
            {
                setImage("player_blue_hurt.png");
            }
            else
            {
                setImage("player_blue.png");
            }
        }
        else
        {
            walkProgress += speed / 2;
            if (invincibilityTimer > 0)
            {
                setImage("player_blue_walk" + ((walkProgress / 10) - 1) + "_hurt.png");
            }
            else
            {
                setImage("player_blue_walk" + ((walkProgress / 10) - 1) + ".png");
            }
        }
 
        if (walkProgress >= 45)
        {
            walkProgress = 10;
        }
 
        World world = getWorld();
        Board board = (Board)world;
        board.playerBlueX = getX();
        board.playerBlueY = getY();
    }
 
    public void useWeapons()
    {
        if (Greenfoot.isKeyDown(weaponKey))
        {
            if (currentWeapon == 1 && swordTimer <= 0)
            {
                //getWorld().addObject(new Sword(this, getRotation(), backSwing, false), getX(), getY());
                if (backSwing)
                {
                    backSwing = false;
                }
                else
                {
                    backSwing = true;
                }
                swordTimer = 20;
            }
            else if (currentWeapon == 2 && gunTimer <= 0)
            {
                getWorld().addObject(new Bullet(0, 4, 20, 10, 8, 600, getArms()), getX(), getY());
                gunTimer = 5;
            }
            else if (currentWeapon == 3 && gunTimer <= 0)
            {
                for (int i = 0; i < 7; i++)
                {
                    getWorld().addObject(new Bullet(1, 5, 75, 8, 30, 25, getArms()), getX(), getY());
                }
                gunTimer = 50;
                knockbackLeft = 30;
                knockbackSpeed = 3;
            }
            else if (currentWeapon == 4 && gunTimer <= 0)
            {
                getWorld().addObject(new Grenade(6, getRotation(), getArms()), getX(), getY());
                gunTimer = 50;
            }
            else if (currentWeapon == 5 && gunTimer <= 0)
            {
                for (int k = 0; k < 3; k++)
                {
                    Bullet bullet = new Bullet(2, 5, 10, 10, 1, 600, getArms());
                    getWorld().addObject(bullet, getX(), getY());
                    bullet.setRotation(getRotation() - 30 + k * 30);
                }
                gunTimer = 40;
            }
            else if (currentWeapon == 6 && isWalking && joustTimer <= 0)
            {
                joustTimer = 80;
            }
        }
        if (Greenfoot.isKeyDown(specialKey))
        {
            if (currentWeapon == 1 && swordTimer <= 0 && spinTimer <= 0)
            {
                //getWorld().addObject(new Sword(this, getRotation(), false, true), getX(), getY());
                backSwing = false;
                swordTimer = 50;
                spinTimer = 620;
                spinProgress = getRotation();
                // getWorld().addObject(new SpinAttackBar(this, spinTimer), getX(), getY());
            }
            else if (currentWeapon == 2 && spinTimer <= 0)
            {
                gunTimer = 30;
                spinTimer = 300;
                spinProgress = getRotation();
            }
            else if (currentWeapon == 5 && knifeTimer <= 0)
            {
                for (int k = 0; k < 5; k++)
                {
                    Bullet bullet = new Bullet(3, 10, 10, 10, 1, 600, getArms());
                    getWorld().addObject(bullet, getX(), getY());
                    bullet.setRotation(getRotation() - 30 + k * 15);
                }
                knifeTimer = 300;
            }
        }
        if (Greenfoot.isKeyDown(itemKey))
        {
            if (currentItem == 1)
            {
                getWorld().addObject(new Medpack(4, getRotation(), 30, true), getX(), getY());
                currentItem = 0;
            }
            else if (currentItem == 2)
            {
                getWorld().addObject(new Bomb(4, getRotation(), 50, true), getX(), getY());
                currentItem = 0;
            }
        }
        if (currentWeapon == 1 && spinTimer > 584)
        {
            spinProgress += 30;
            setRotation(spinProgress);
            spinUpdate();
        }
        if (currentWeapon == 2 && spinTimer > 282)
        {
            spinProgress += 20;
            setRotation(spinProgress);
            spinUpdate();
            getWorld().addObject(new Bullet(0, 10, 20, 10, 8, 600, getArms()), getX(), getY());
        }
        /*
        if (Greenfoot.isKeyDown("shift") && shieldTimer <= 0)
        {
        shieldTimer = 2;
        swordTimer = 1;
        getWorld().addObject(new Shield(this, getRotation(), shieldTimer), getX(), getY());
        }
         */
 
        if (swordTimer > 0)
        {
            swordTimer--;
        }
        if (spinTimer > 0)
        {
            spinTimer--;
        }
        if (gunTimer > 0)
        {
            gunTimer--;
        }
        if (knifeTimer > 0)
        {
            knifeTimer--;
        }
        if (joustTimer > 0)
        {
            joustTimer--;
        }
    }
 
    public Player_Arms getArms()
    {
        return null;
    }
 
    public void updateArms()
    {
    }
 
    public void spinUpdate()
    {
    }
 
    public void die()
    {
    }
}
Here's my zombie code:
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Zombie here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Zombie extends Actor
{
    public int walkProgress = 10;
    public int speed = 1;
    public int lives = 10;
    public int invincibilityTimer = 0;
    public int knockbackLeft = 0;
    public int knockbackSpeed = 0;
    public boolean droppedBomb = false;
    public int lungeRange = 50;
    public boolean grenade = false;
    public int grenadeTimer = 0;
    public Player_Arms arms;
    private static final GreenfootImage[] walkingFrames = {
            new GreenfootImage("zombie_walk0.png"),
            new GreenfootImage("zombie_walk1.png"),
            new GreenfootImage("zombie_walk2.png"),
            new GreenfootImage("zombie_walk3.png"),
            new GreenfootImage("zombie_walk0_hurt.png"),
            new GreenfootImage("zombie_walk1_hurt.png"),
            new GreenfootImage("zombie_walk2_hurt.png"),
            new GreenfootImage("zombie_walk3_hurt.png"),
            new GreenfootImage("zombie_scratch0.png"),
            new GreenfootImage("zombie_scratch1.png"),
            new GreenfootImage("zombie_scratch2.png"),
            new GreenfootImage("zombie_scratch3.png"),
        };
 
    /**
     * Act - do whatever the Zombie wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        Board board = (Board)getWorld();
 
        if (board.playing)
        {
            if (lives > 0)
            {
                if (!grenade)
                {
                    arms = null;
                }
                if (knockbackLeft <= 0)
                {
                    moveAround();
                }
                else
                {
                    setImage(walkingFrames[(walkProgress / 10) - 1 + 4]);
                    turn(180);
                    move(knockbackSpeed);
                    knockbackLeft -= knockbackSpeed;
                    turn(-180);
                }
                hurtPlayers();
                if (invincibilityTimer > 0)
                {
                    invincibilityTimer--;
                }
                if (grenadeTimer > 0)
                {
                    grenadeTimer--;
                }
                else if (grenade)
                {
                    board.addObject(new ExplosionSmall(false), getX(), getY());
                    for (int s = 0; s < 20; s++)
                    {
                        board.addObject(new Bullet(1, 10, 75, 8, 180, 20, arms), getX(), getY());
                    }
                    arms.player.combo += 1;
                    arms.player.comboTimer = 60;
                    if (arms.comboDisplayTimer <= 0)
                    {
                        board.addObject(new ComboDisplay(arms.player), arms.player.getX(), arms.player.getY());
                        arms.comboDisplayTimer = 10;
                    }
                    lives = 0;
                }
                else
                {
                    arms = null;
                }
            }
            else
            {
                arms = null;
                int weaponDrop = Greenfoot.getRandomNumber(6) + 1;
                while (weaponDrop == board.playerBlueWeapon && weaponDrop == board.playerRedWeapon)
                {
                    weaponDrop = Greenfoot.getRandomNumber(4) + 1;
                }
                if (Greenfoot.getRandomNumber(20) < 1 && !board.weaponsInWorld())
                {
                    board.addObject(new Weapon(weaponDrop), getX(), getY());
                }
                else if (Greenfoot.getRandomNumber(100) < 1)
                {
                    board.addObject(new Medpack(0, 0, 0, false), getX(), getY());
                }
                else if (Greenfoot.getRandomNumber(1000) < 5)
                {
                    board.addObject(new Bomb(0, 0, 0, false), getX(), getY());
                }
                board.removeObject(this);
            }
        }
    }
 
    public void moveAround()
    {
        Board board = (Board)getWorld();
 
        int playerBlueDistance = (int)Math.sqrt((getX() - board.playerBlueX)*(getX() - board.playerBlueX)+(getY() - board.playerBlueY)*(getY() - board.playerBlueY));
        int playerRedDistance = (int)Math.sqrt((getX() - board.playerRedX)*(getX() - board.playerRedX)+(getY() - board.playerRedY)*(getY() - board.playerRedY));
        if (board.playerBlueAlive && board.playerRedAlive)
        {
            if (playerRedDistance < playerBlueDistance)
            {
                if (!grenade)
                {
                    turnTowards(board.playerRedX, board.playerRedY);
                    move(speed);
                }
                walkProgress += speed;
            }
            else
            {
                if (!grenade)
                {
                    turnTowards(board.playerBlueX, board.playerBlueY);
                    move(speed);
                }
                walkProgress += speed;
            }
            if (playerRedDistance < lungeRange || playerBlueDistance < lungeRange)
            {
                if (!grenade)
                {
                    move(speed);
                    move(speed);
                    walkProgress += speed;
                }
            }
        }
        else if (board.playerBlueAlive && !board.playerRedAlive)
        {
            if (!grenade)
            {
                turnTowards(board.playerBlueX, board.playerBlueY);
                move(speed);
            }
            walkProgress += speed;
            if (playerBlueDistance < lungeRange)
            {
                if (!grenade)
                {
                    move(speed);
                    move(speed);
                    walkProgress += speed;
                }
            }
        }
        else if (!board.playerBlueAlive && board.playerRedAlive)
        {
            if (!grenade)
            {
                turnTowards(board.playerRedX, board.playerRedY);
                move(speed);
            }
            walkProgress += speed;
            if (playerRedDistance < lungeRange)
            {
                if (!grenade)
                {
                    move(speed);
                    move(speed);
                    walkProgress += speed;
                }
            }
        }
 
        if (invincibilityTimer > 0)
        {
            setImage(walkingFrames[(walkProgress / 10) - 1 + 4]);
        }
        else if (grenade)
        {
            setImage(walkingFrames[(walkProgress / 10) - 1 + 8]);
        }
        else
        {
            setImage(walkingFrames[(walkProgress / 10) - 1]);
        }
 
        if (walkProgress >= 45)
        {
            walkProgress = 10;
        }
    }
 
    public void hurtPlayers()
    {
        Board board = (Board)getWorld();
 
        Player playerBlue = (Player)getOneIntersectingObject(Player_Blue.class);
        Player playerRed = (Player)getOneIntersectingObject(Player_Red.class);
        Shield shield = (Shield)getOneIntersectingObject(Shield.class);
        if (withinRange(board.playerBlueX, board.playerBlueY, 10, 10))
        {
            if (playerBlue != null && playerBlue.lives > 0)
            {
                knockbackLeft = 20;
                knockbackSpeed = 8;
                playerBlue.lives--;
                playerBlue.comboTimer = 0;
                playerBlue.invincibilityTimer = 10;
                playerBlue.setRotation(getRotation());
                playerBlue.turn(180);
                playerBlue.knockbackLeft = 20;
                playerBlue.knockbackSpeed = 8;
                getWorld().addObject(new AttackEffectRed(playerBlue.getRotation()), playerBlue.getX(), playerBlue.getY());
            }
        }
        if (withinRange(board.playerRedX, board.playerRedY, 10, 10))
        {
            if (playerRed != null && playerRed.lives > 0)
            {
                knockbackLeft = 20;
                knockbackSpeed = 8;
                playerRed.lives--;
                playerRed.comboTimer = 0;
                playerRed.invincibilityTimer = 10;
                playerRed.setRotation(getRotation());
                playerRed.turn(180);
                playerRed.knockbackLeft = 20;
                playerRed.knockbackSpeed = 8;
                getWorld().addObject(new AttackEffectRed(playerRed.getRotation()), playerRed.getX(), playerRed.getY());
            }
        }
    }
 
    public boolean withinRange(int posX, int posY, int radiusX, int radiusY)
    {
        if (getX() > posX - radiusX && getX() < posX + radiusX && getY() > posY - radiusY && getY() < posY + radiusY)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
 
    public boolean shieldBlocking(int rotation, int range)
    {
        rotation += 180;
        if (rotation > 360)
        {
            rotation -= 360;
        }
        if (getRotation() > rotation - range && getRotation() < rotation + range)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
Does anyone know what I'm doing wrong?
K_wow K_wow

2014/8/28

#
Please? Can I have some help with this?
danpost danpost

2014/8/29

#
'glitch' is quite vague. Please elaborate. Express what behavior is wanted and what current behavior is not.
K_wow K_wow

2014/8/29

#
The zombies are being moved by the player's knockback, as though it's their own knockback. Ideally the zombies would only be moved by their own knockback and not the player's.
davmac davmac

2014/8/29

#
Zombies won't be directly affected by the variables and logic in another class. What you're saying isn't really possible. Is it perhaps the case that both the player and the zombie are getting knocked back at the same time? Is this the scenario - http://www.greenfoot.org/users/9686 - ? since I don't see the problem happening.
K_wow K_wow

2014/8/29

#
Firstly, yes I realize that this is not possible. There must be some other way that the zombies are being affected. The player and any intersecting zombies will knock each other back, yes, but every single zombie on the map is being affected, not just the intersecting ones. What's more, other actions like the player shooting a shotgun, which also affect the player's knockback, will knock all existing zombies as well. It is indeed the scenario. The reason that you are not seeing the problem is because it only happens in multiplayer, and even then it is an uncommon glitch. I have now altered the code to make the knockback variables from both the player and the zombie to make them private. Hopefully this will fix the problem, but I haven't tested it enough yet.
K_wow K_wow

2014/8/30

#
Even after I edited the code to make the knockback of zombies and players private, the zombies are still being moved by the player's knockback.
danpost danpost

2014/8/30

#
I would comment out the code for the player (or just change the name of the act method) and then check the behavior of the zombies to see if it is really the knockback of the player that is causing the zombies to be moved.
K_wow K_wow

2014/8/31

#
I commented out the lines that set the players knockback and the glitch no longer happened. I added the lines back in and it straight away happened again. I have also noticed that once the red player dies the glitch no longer happens. The zombies are only affected by the blue player's knockback.
K_wow K_wow

2014/8/31

#
Ah, I finally fixed it. It turns out that the player base class was always setting the playerBlueX variable of the world to it's X coordinate, and doing the same with the Y coordinate. In short, both the blue player and the red player would have been setting the world's blue player coordinates to their own in their move code, which they don't execute when they are being knocked back. Thanks for the help anyway! :)
danpost danpost

2014/8/31

#
Your blue player and red player are actors that have a 'getX' and a 'getY' method for getting the current coordinates. Having fields for the blue player and the red player would be much more useful in your world class than having multiple fields for the individual states of each actor. Also, this will avoid having to update the value of the fields each act cycle.
K_wow K_wow

2014/8/31

#
danpost wrote...
Your blue player and red player are actors that have a 'getX' and a 'getY' method for getting the current coordinates. Having fields for the blue player and the red player would be much more useful in your world class than having multiple fields for the individual states of each actor. Also, this will avoid having to update the value of the fields each act cycle.
Are you saying that the player would update the world's player location instead of it's own? How would that help?
danpost danpost

2014/8/31

#
K_wow wrote...
Are you saying that the player would update the world's player location instead of it's own? How would that help?
No. I am saying that with player references, you do not need location fields at all. Instead of:
1
2
3
4
int x;
int y;
int X;
int Y;
you would have:
1
2
Actor redPlayer;
Actor bluePlayer;
and then locally you can use:
1
int x = redPlayer.getX(); // etc
And, if needed, you can add 'get' methods to return each player.
K_wow K_wow

2014/9/1

#
Ah, I see.
You need to login to post a reply.