Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (488)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  [Slick2D] Strange collision glitch.  (Read 1001 times)
0 Members and 1 Guest are viewing this topic.
Offline Jacob Pickens
« Posted 2014-03-05 23:59:01 »

So heres the glitch:

Click to Play


The player runs right through the zombie. Then after a few seconds it responds to the collision. That looks terrible for one, and also you could still shoot after you were obviously dead. It only happens when you walk directly towards the zombie though. Otherwise its quite responsive.

I'm using the slick rectangles along with the slick intersects.

So whats the problem. What am I doing wrong? Is it a bunch of lag?

I don't know what is making it do this but I do know that it's not going to work.

Code:
Player:
1  
2  
3  
4  
5  
...
public Rectangle getBounds() {
return new Rectangle((int)x, (int)y, main.getWidth(), main.getHeight());
}
...


Zombie:
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 update(int delta) {
     
   collision();
     
   main.update(delta);
     
   if(right) {
      x += .13;
   }else{
      x -= .13;
   }
}
   
public void collision() {
   if(getBounds().intersects(p.getBounds())) {
      System.exit(0); //This is not final
       }
}

public Rectangle getBounds() {
return new Rectangle((int)x, (int)y, main.getWidth(), main.getHeight());
}
...



Edit: And if anyone cares... I'm using Eclipse KEPLER with the Dark UI mod.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #1 - Posted 2014-03-06 00:01:13 »

Please post your code! We are going to need to see how you assign collision boxes to the zombies and player. Do you update the position of the collision boxes?

Offline Jacob Pickens
« Reply #2 - Posted 2014-03-06 00:06:12 »

Added the code tht mattered to the collisions. If you need more ill just post the  source. WARNING: IT IS REALLY MESSY
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Rayvolution

JGO Kernel


Medals: 186
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #3 - Posted 2014-03-06 00:20:46 »

Try rendering your collision box, I suspect its not actually centered on the player. Shocked

EDIT: Or not centered on the zombies.

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Offline Jacob Pickens
« Reply #4 - Posted 2014-03-06 00:22:30 »

Good idea ray I'll do it right now... I'll post the result in a .gif
Online Rayvolution

JGO Kernel


Medals: 186
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #5 - Posted 2014-03-06 00:28:31 »

I suggest adding a debug mode to your game to toggle your collision boxes (or any other data you want) on keypress. Thats what I usually do, it's really helpful Cheesy

Example:

in render();
(This is the code ripped right out of my game, so obviously it won't all work for you. Just change everything in the IF statement to whatever you want to display)
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  
      //If debug mode is turned on, render the following data.
     if(debugMode){
         font.drawString(5, 15, "FPS: " + gc.getFPS());
         font.drawString(5, 25, "Player X: " + playerX + " Y: " + playerY,0,255,0);
         font.drawString(5, 35, "Map X: " + mapX + " Y: " + mapY,0,0,255);
         font.drawString(5, 45, "Tile X: " + getPlayerTileLocationX() + " Y: " + getPlayerTileLocationY(),0,255,255);
         font.drawString(5, 55, "Map: " + getMapName(),255,255,0);
         font.drawString(5, 65, "Map Layer 0: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 0));
         font.drawString(5, 75, "Map Layer 1: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 1));
         font.drawString(5, 85, "Map Layer 2: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 2));
         font.drawString(5, 95, "Map Layer 3: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 3));
         font.drawString(5, 105, "Map Layer 4: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 4));
         font.drawString(5, 115, "Map Layer 5: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 5));
         font.drawString(5, 125, "Map Layer 6: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 6));
         font.drawString(5, 135, "Map Layer 7: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 7));
         font.drawString(5, 145, "Map Layer 8: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 8));
         font.drawString(5, 155, "Map Layer 9: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 9));
         font.drawString(5, 165, "Map Layer 10: " + getTileID(getPlayerTileLocationX(), getPlayerTileLocationY(), 10));
         font.drawString(5, 185, "Save Tick: " + saveTick);
         font.drawString(5, 205, mouseX + " " + mouseY);
         //Render collision boxes.
        for (int i = 0; i < COLLISION_ARRAY.size(); i++) {
            COLLISION_ARRAY.get(i).render(g);
         }
      }


then just under update(); just toggle the debug boolean true/false on a keypress

In update();
(NOTE: change SettingsParser.getKeyDebug to INPUT_<keyyouwant>)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
         if(input.isKeyPressed(SettingsParser.getKeyDebug())){
            if (getDebugMode()){
               setDebugMode(false);
               input.clearKeyPressedRecord();
               return;
            }else{
               setDebugMode(true);
               input.clearKeyPressedRecord();
               return;
            }
         }

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Offline Jacob Pickens
« Reply #6 - Posted 2014-03-06 00:31:21 »

Alright the same thing happened. What do I do?

Click to Play
Online Rayvolution

JGO Kernel


Medals: 186
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #7 - Posted 2014-03-06 00:39:13 »

hrm.. does this happen as soon as you release the key, or literally just after a few seconds?

Does any of the IF statements in your code that fire in your code have a return; statement? If you have a return; statement in your input code, it'll override/ignore the collision detection if collision(); is under them but suddenly fire as soon as the key released. (it's an odd glitch in slick I think)

We might need to see your input control code as well, or whatever is one step higher than the update() method you showed us.

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Offline Jacob Pickens
« Reply #8 - Posted 2014-03-06 00:42:13 »

What's above tht is render... There are no return statements... I rlly just think is a lag thing... Imma just screw slick and go boilerplate again... Everything is harder but it seems to work better... Or maybe you could recommend a better library?
Online Rayvolution

JGO Kernel


Medals: 186
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #9 - Posted 2014-03-06 00:44:07 »

What's above tht is render... There are no return statements... I rlly just think is a lag thing... Imma just screw slick and go boilerplate again... Everything is harder but it seems to work better... Or maybe you could recommend a better library?

but something has to be turning on that "right" boolean? Shocked

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jacob Pickens
« Reply #10 - Posted 2014-03-06 00:46:18 »

Thts in the constructor. It basically checks what side of the player it on so tht I don't have to make a right zombie and a left zombie
Online Rayvolution

JGO Kernel


Medals: 186
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #11 - Posted 2014-03-06 00:50:25 »

ohh, I see. This is code for the *zombie*. I thought this was attached to the *player*.

Well in that case, I am honestly clueless what is going on. You must have some wacky code somewhere you didn't post, that or I'm having a blind-moment.

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Online BurntPizza
« Reply #12 - Posted 2014-03-06 00:51:03 »

Does Slick use bottom-up coordinates, or top-down like J2D? If it uses bottom-up and you are using J2D rectangles, wouldn't the rectangles then be in the ground? Just something to check. (Doubt it would cause this problem, but still check that)

Also, your getBounds() creates a rectangle object every call, probably not a great idea, esp. if you plan on having lots of zombies.

Unfortunately I don't yet see why this is happening.

What is that main.update()?
Offline Jacob Pickens
« Reply #13 - Posted 2014-03-06 00:53:39 »

Ya slick has it's own rectangle class tht I assume is exactly like the J2D one. And yes I know. I had it in its own var but I changed back to the get bounds method to make it more readable.
Online Rayvolution

JGO Kernel


Medals: 186
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #14 - Posted 2014-03-06 00:55:49 »

Does Slick use bottom-up coordinates, or top-down like J2D?

Top (0) -> Bottom (Total Height)

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Online BurntPizza
« Reply #15 - Posted 2014-03-06 00:56:17 »

I had it in its own var but I changed back to the get bounds method to make it more readable.

Just store a private rect and return it in getBounds(). More readable and efficient.


Also, perhaps the zombie's update() isn't called often? What happens if the collision check is in the player class?

Also try a println() statement and watch for it instead of exit()ing if you haven't, it might just be that the VM doesn't exit right away.
Offline Jacob Pickens
« Reply #16 - Posted 2014-03-06 00:58:48 »

I didn't think of that. Thanks so much BurntPizza +1. I'll do that after I'm done watching Harry Potter. Tongue Thanks to you too ray, ur my go to guy when it comes to slick2D.

I tried the println method, it also has a lag time.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #17 - Posted 2014-03-06 01:03:20 »

Yeah, it might take the JVM a few seconds to deallocate all the data and actually send the shutdown signal. Just to elaborate on what BurntPizza said.

Online Rayvolution

JGO Kernel


Medals: 186
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #18 - Posted 2014-03-06 01:04:40 »

Yeah, it might take the JVM a few seconds to deallocate all the data and actually send the shutdown signal. Just to elaborate on what BurntPizza said.

Wouldn't the game theoretically freeze while it's doing that though?

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Online BurntPizza
« Reply #19 - Posted 2014-03-06 01:05:53 »

Quote
It only happens when you walk directly towards the zombie though. Otherwise its quite responsive.

My guess is that some event handling or something is preventing the VM from exiting, aka while key is being held down for movement, but that's just a hunch.
Offline Jacob Pickens
« Reply #20 - Posted 2014-03-06 01:07:36 »

Tht wouldn't be it, because even if I don't have shutdown there... Just a println it still lags
Online Rayvolution

JGO Kernel


Medals: 186
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #21 - Posted 2014-03-06 01:09:49 »

Quote
It only happens when you walk directly towards the zombie though. Otherwise its quite responsive.

My guess is that some event handling or something is preventing the VM from exiting, aka while key is being held down for movement, but that's just a hunch.

Yeah, that's what I was thinking. The gc.exit(); is firing as soon as the key is released, I had a similar problem in my game. That's one of the reasons I added input.clearKeyPressedRecord(); to my keystrokes. You can still "hold down" keys but as the game is polling it'll allow certain actions to happen that otherwise may be blocked out. From how I understand it, it's basically dumping the key input buffer constantly, allowing actions to happen before the key is put back in the buffer.

There may be a much better way to do it, but it works like a champ for me. I've had no reason to do it differently.

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Offline The Lion King
« Reply #22 - Posted 2014-03-06 01:24:07 »

Quote
It only happens when you walk directly towards the zombie though. Otherwise its quite responsive.

Can you elaborate on this? If collisions work while the player is not moving, but "lags" when the player is moving, then i think the problem lies in the player. I can come up with a lot of different things that can be wrong if this is the case, but instead of speculating would it be possible to post a little more code on the player class.

"You have to want it more than you want to breath, then you will be successful"
Offline Jacob Pickens
« Reply #23 - Posted 2014-03-06 01:28:16 »

Ya sure thing I'll give the entire class on second
Offline Jacob Pickens
« Reply #24 - Posted 2014-03-06 01:30:25 »

Entire Player Class:
WARNING: Quite messy

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  
package com.pickens.objects;

import java.awt.Font;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.state.StateBasedGame;

import com.pickens.objects.controllers.ArcadeCounter;
import com.pickens.objects.controllers.BulletController;
import com.pickens.objects.controllers.ZombieController;
import com.pickens.src.Game;

public class Player {

   //TODO: Fix Animations
 
   float x = 640/2-28;
   float y = 256-128;
   
   //TODO: Change this to more reasonable number when ammo drops are available
 
   int ammo = 100;
   
   double velX = 0;
   double velY = 0;
   
   int[] duration = {150, 150};
   
   public Rectangle bounds;
   
   BulletController bm;
   ZombieController zc;
   ArcadeCounter ac;
   Input input;
   
   Animation lt;
   Animation rt;
   Animation rti;
   Animation lti;
   Animation main;
   
   //TODO: Better Gun Management when there are more weapons!
  Image gun;
   boolean shouldFire = false;
   
   boolean moving = false;
   
   boolean l = false;
   boolean r = true;
   
   boolean ml = false, mr = false;
   
   int xpos;
   
   Font awtFont;
   TrueTypeFont font;
   
   public Player(ArcadeCounter ac, BulletController bm, ArcadeCounter ac1, ZombieController zc) throws SlickException {
      this.ac = ac1;
      this.bm = bm;
      this.zc = zc;
      init();
   }
   
   public void init() throws SlickException {
      Image[] lt = {new Image("res/ZN_Left1.png"), new Image("res/ZN_Left2.png")};
      Image[] rt = {new Image("res/ZN_Right1.png"), new Image("res/ZN_Right2.png")};
      Image[] lti = {new Image("res/ZN_Left Idle.png"), new Image("res/ZN_Left Idle.png")};
      Image[] rti = {new Image("res/ZN_Right Idle.png"), new Image("res/ZN_Right Idle.png")};
     
      this.lt = new Animation(lt, duration, false);
      this.rt = new Animation(rt, duration, false);
      this.rti = new Animation(rti, duration, false);
      this.lti = new Animation(lti, duration, false);
     
      main = this.rti;
     
      gun = new Image("res/Pistol.png");
     
      awtFont = new Font("Arial", Font.BOLD, 12);
      font = new TrueTypeFont(awtFont, false);
     
      bounds = new Rectangle((int)x, (int)y, main.getWidth(), main.getHeight());
     
      ac.start();
   }
   
   public void render(Graphics g) throws SlickException {
      //other objects' renders
     main.draw((int) x, (int) y);
      bm.render(g);
     
      //This objects render
     if(r)
         g.drawImage(gun, (int) x + 28, (int) y + 20);
      if(l)
         g.drawImage(gun.getFlippedCopy(true, false), (int) x - 10, (int) y + 20);
      else
         g.drawImage(gun, (int) x + 28, (int) y + 20);
      g.setColor(Color.white);
      g.setFont(font);
      g.drawString("Ammo: " + ammo, 12, 12);
      g.setColor(Color.red);
     
      g.draw((Shape)getBounds());
   }
   
   public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException, InterruptedException {
      //Other objects' updates
     main.update(delta);
      bm.update(gc, sbg, delta);
     
      input = gc.getInput();
     
      xpos = Mouse.getX();
     
      if(xpos < x) {
         ml = true;
         mr = false;
      }
      if(xpos > x) {
         mr = true;
         ml = false;
      }
     
      if(moving)
         x += velX;
     
      //Important Area
     if(ac.delete) {
         collision();
         movement();
         fire();        
      }
   }
   
   public void movement() {
      if(input.isKeyDown(Input.KEY_RIGHT)) {
         moving = true;
         l = false;
         r= true;
         main = rt;
         main.start();
         velX = .13;
      }else
      if(input.isKeyDown(Input.KEY_LEFT)) {
         moving = true;
         r = false;
         l = true;
         main = lt;
         velX = -.13;
      }else{
         if(main == rt && main != rti && main != lti) {
            main = rti;
            l = false;
         }else if(main == lt && main != rti && main != lti){
            main = lti;
            r = false;
         }
         moving = false;
         velX = 0;
      }
   }
   
   public void fire() throws SlickException {
      //Disable gun if ammo == 0
     if(ammo == 0) {
         shouldFire = false;
      }
     
      //Shooting
     if(input.isMouseButtonDown(0)) {
         if(shouldFire && ml) {
            if(xpos > x) {
               bm.addBullet(new Bullet(x-2, y + 23,0,bm));
               ammo--;
               shouldFire = false;
               return;
            }else{
               if(moving)
                  main = lt;
               else
                  main = lti;
               r = false;
               l = true;
               bm.addBullet(new Bullet(x-2, y + 23,0,bm));
               ammo--;
               shouldFire = false;
               return;
            }
         }  
         if(shouldFire && mr) {
            if(xpos < x) {
               bm.addBullet(new Bullet(x+36, y + 23,1,bm));
               ammo--;
               shouldFire = false;
               return;
            }else{
               if(moving)
                  main = rt;
               else
                  main = rti;
               l = false;
               r = true;
               bm.addBullet(new Bullet(x+36, y + 23,1,bm));
               ammo--;
               shouldFire = false;
               return;
            }
         }
      }else{
         shouldFire = true;
      }
   }
   
   public void collision() {
      if(x < 0) {
         velX = .13;
      }else if(x > Game.WIDTH - main.getWidth()){
         velX = -.13;
      }
   }
   
   public Rectangle getBounds() {
      return new Rectangle((int)x, (int)y, main.getWidth(), main.getHeight());
   }
   
}
Offline Jacob Pickens
« Reply #25 - Posted 2014-03-06 01:31:40 »

Entire Zombie Class:

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  
package com.pickens.objects;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;

public class Zombie {

   float x, y;
   
   Player p;
   
   int[] duration = {200, 200};
   
   Animation lt;
   Animation rt;
   
   boolean left = false, right = false;
   
   Animation main;
   
   Rectangle bounds;
   
   public Zombie(float x, float y, Player p) throws SlickException {
      this.x = x;
      this.y = y;
      this.p = p;
     
      init();
   }
   
   public void init() throws SlickException {
      Image[] lt = {new Image("res/Zombie_Left1.png"), new Image("res/Zombie_Left2.png")};
      Image[] rt = {new Image("res/Zombie_Right1.png"), new Image("res/Zombie_Right2.png")};
     
      this.lt = new Animation(lt, duration, false);
      this.rt = new Animation(rt, duration, false);
     
      if(x < p.x) {
         main = this.rt;
         right = true;
      }else{
         main = this.lt;
         left = true;
      }
     
     
   }
   
   public void render(Graphics g) {
      main.draw((int) x, (int) y);
      g.setColor(Color.red);
      g.draw(getBounds());
   }
   
   public void update(int delta) {
     
      collision();
     
      main.update(delta);
     
      if(right) {
         x += .13;
      }else{
         x -= .13;
      }
   }
   
   public void collision() {
      if(getBounds().intersects(p.getBounds())) {
         System.exit(0);
      }
   }
   
   public Rectangle getBounds() {
      return new Rectangle((int)x, (int)y, main.getWidth(), main.getHeight());
   }
   
}


Along with the entire ZombieController class:

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  
package com.pickens.objects.controllers;

import java.util.ArrayList;
import java.util.Random;

import javax.swing.Timer;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import com.pickens.objects.Player;
import com.pickens.objects.Zombie;

public class ZombieController {

   ArrayList<Zombie> zombies = new ArrayList<Zombie>();
   
   Player p;
   
   Zombie TempZombie;
   
   float number = 0;
   
   long last = System.currentTimeMillis();
   
   public ZombieController(Player p) {
      this.p = p;
   }
   
   public void render(Graphics g) {
      for(int i = 0; i < zombies.size(); i++) {
         TempZombie = zombies.get(i);
         
         TempZombie.render(g);
      }
   }
   
   public void update(int delta) throws SlickException {
      number += .1;
      if(number >= 200) {
         spawn();
         System.out.println("Zombie Spawn Possbile");
         number = 0;
      }
      for(int i = 0; i < zombies.size(); i++) {
         TempZombie = zombies.get(i);
         
         TempZombie.update(delta);
      }
   }
   
   public void spawn() throws SlickException {
      Random r = new Random();
      if(r.nextInt(99) < 29) { //30% Chance left
        addZombie(new Zombie(640+64,256-128,p));
      }
      if(r.nextInt(99) < 29) { //30% Chance right
        addZombie(new Zombie(-64,256-128,p));
      }
   }
   
   public void addZombie(Zombie e) {
      zombies.add(e);
   }
   
   public void removeZombie(Zombie e) {
      zombies.remove(e);
   }
   
}
Online Rayvolution

JGO Kernel


Medals: 186
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #26 - Posted 2014-03-06 01:41:57 »

<snippy>

Just a quick note, although this is unrelated, are all your images enlarged/upscaled images? (IE: You took a 16x16 image and enlarged it to 64x64? So the *actual* image file is 64x64?) Because I don't see a g.scale() anywhere in your render method and you're getting your window heights/widths via main.getWidth() and main.getHeight(). When you use getWidth and getHeight in slick, it will return the *unscaled* Width and Height, not scaled. So when you scale, you have to divide the getWidth and getHeight by how much you scaled it. Thus, I suspect you're not scaling anything. You may be loading images a *lot* larger than they need to be to get the desired effect you want.

For example, if you use:
g.scale(3,3); // 300% zoom
then.. if you need the actual height and width you would use:
gc.getHeight()/3, gc.getWidth()/3 //A third of the height/width.

If you didn't know, if you put g.scale(2,2); (for 200% zoom, for example) at the top of your render method it will scale the entire game, so you don't have to make "enlarged" versions of smaller images. Keep in mind if you do this though, everything will be blurred and you'll have to apply a nearest neightbor filter to all your images:
ExampleImage = new Image("res/somepic.png", false, Image.FILTER_NEAREST);

EDIT: Also, you dont have to apply g.scale to everything, if you want you can apply it only to certain objects:
//Stuff you DONT want scaled
g.scale(2,2); //for 200% scaling
//Stuff you DO want scaled
g.scale(1,1);
// More stuff you dont want scaled.

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Offline Jacob Pickens
« Reply #27 - Posted 2014-03-06 01:46:35 »

Ya honestly I have no idea what is going on With this glitch... I think I will swap the collision detection two the player to c if the zombie update is called unreliably, I hope I can fix this... I rlly don't wanna go boilerplate again
Online BurntPizza
« Reply #28 - Posted 2014-03-06 01:54:39 »

Maybe you're doing other stuff with it elsewhere, but this stands out to me:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
//from Zombie.java:
if(x < p.x) {
      main = this.rt;
      right = true;
}else{
      main = this.lt;
      left = true;
}

//and

if(right) {
      x += .13;
}else{
      x -= .13;
}


Why is there a left? Also, is right ever set to false instead of left being set true? Because left is never tested, only right is, and I don't see it ever set to false. (Other than being false from it's declaration)


Where is ZombieController.update() called, and how often? (Presumably every frame)

I think I will swap the collision detection two the player

Yeah, I would have a isZombieTouching() or something in the player's update method, makes more sense than for every zomb to be doing it, at least to me.
Offline Jacob Pickens
« Reply #29 - Posted 2014-03-06 02:01:50 »

Honestly I have no idea about the booleans  Tongue the zombie controller is called in the play state every time the play state update is called... Idk what the problem is ugh
Pages: [1] 2
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (17 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (69 views)
2014-08-11 02:49:23

BurntPizza (46 views)
2014-08-09 21:09:32

BurntPizza (37 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!