Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1] 2
1  Games Center / Showcase / Re: SmileyAdventure on: 2014-12-18 18:39:02
Would love to try it, but 116MB download?  No thanks.
2  Java Game APIs & Engines / Engines, Libraries and Tools / Re: LibGDX Othographic Camera Problem on: 2014-12-17 21:27:33
From what I can see, you initialize the cam in your Main class AFTER initializing the GameStateHandler.  If the GameStateHandler creates and intializes the Play state, then your call to getCam() would return null because the Main class had not initialized the camera yet!  Your posted code doesn't show where and when the Play state is set up but I'm betting that's it.

3  Game Development / Newbie & Debugging Questions / LibGDX - How to rotate one point around another on: 2014-12-17 19:59:45
Hi guys,

I am really close to solving this issue, but my math skills are lacking so I need some input.  I have a player, that player has a separate "arm" object that can swing and show the selected item the player is holding.  I have the arm swinging motion working fine but now I am trying to display the selected item in his "hand" swinging around the same origin point that his arm is attached.  See the video below for the problem.

<a href="http://www.youtube.com/v/A6MYCWTN4ZU?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/A6MYCWTN4ZU?version=3&amp;hl=en_US&amp;start=</a>

I've slowed his swing speed quite a bit to illustrate the problem and there is a noticeable gap between the item and his arm at certain points of the rotation.  I think the fact that LibGDX offsets the rectangle of the item by originX and originY is what is messing it up, but I don't know how to fix it.  This code determines the position of the arm and the item:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public void update(float dt)
{
   selectedItem = player.getSelectedItem();

        // This set the position of the arm itself
   position.set(player.position.x + 0.3f, player.position.y + 0.4f);

        // This code sets the items position to rotate around the arm position
        // height is the length of the arm which I'm using as the radius of the "circle"
   itemPosition.set(position.x + height * (float)Math.cos((rotation - 90) * MathUtils.degRad), position.y + height * (float)Math.sin((rotation - 90) * MathUtils.degRad));
     
}


This code renders everything which is where I believe I'm going wrong:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public void render(SpriteBatch sb)
{
        // This one draws the arm and works perfectly.  I change the originX to half width
   sb.draw(region, position.x, position.y, width * 0.5f, height, width, height, 1, 1, rotation);

        // This draws the selected item sprite according to the same rotation and where I believe the issue is occuring
   if(selectedItem != null)
   {
      sb.draw(Game.res.getTextureRegion(selectedItem.getID()),
            itemPosition.x, itemPosition.y,
            0.2f, 0.6f,  // originX and originY - Been playing with these to get close to desired effect
            0.6f, 0.6f,  // width and height of item texture region
            1,1,
            rotation);
      }
   }


I should mention that I am using a Y-DOWN coordinate system with my batch.  Really banging my head with this one!  Thanks in advance.
4  Game Development / Newbie & Debugging Questions / Re: Gravity. Player shakes when jumping because of contradicting velocity/ on: 2014-12-09 17:06:07
I am using a similar method called .overlaps() from the LibGDX Rectangle class.  Works very similarly to the Java version.  Glad you got it working.
5  Game Development / Newbie & Debugging Questions / Re: Libgdx OrthographicCamera - How to set graphical objects always to center? on: 2014-12-06 18:13:32
By default the origin of the OrthographicCamera is the center of it's view.  So camera.position.x and camera.position.y would be where to draw your objects that you want centered.
6  Game Development / Newbie & Debugging Questions / LibGDX - Viewport messing with my UI projection matrix! on: 2014-12-06 17:20:43
Hi guys.  I have a main camera set to orthographic projection sized 30x30 (world units).  My game window if resizable and my world is tile based so it seemed the right way to do it.  Then I quickly realized that fonts were HUGE when attempting to render with this camera projection matrix, so I made a new one:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
   // Set up normal projection for font rendering
     
   normalProjection = new Matrix4().setToOrtho2D(0, 0, Gdx.graphics.getWidth(),  Gdx.graphics.getHeight())
        ....

       // during render after world with main camera is done with the batch

       sb.setProjectionMatrix(normalProjection);
       sb.begin();
       debugFont.draw(sb, "x: " + (int) player.position.x, 10, 54);
       sb.end();


And I update it when the window is resized:

1  
2  
3  
4  
5  
6  
@Override
   public void resize(int w, int h)
   {
      world.resize(w, h);
      normalProjection.setToOrtho2D(0, 0, w, h);
   }


This worked fine until I decided to add a FillViewPort to my world for the main camera to use in order to prevent my tiles from being stretched.  After doing that, my text cannot be seen even though (I think) the viewport has nothing to do with my normalProjection Matrix4!  This one has me stumped any ideas?
7  Game Development / Newbie & Debugging Questions / Re: LibGDX - how to make a popup window on: 2014-12-05 17:16:57
@Endos that sounds great I guess I'm more interested in the logic of how it would work.  Specifically pausing the underlying level.  I appreciate the suggestion, maybe I will start there and just see where it takes me.
8  Game Development / Newbie & Debugging Questions / Re: Gravity. Player shakes when jumping because of contradicting velocity/ on: 2014-12-05 15:54:59
I haven't looked at your project files, but I don't think the gravity is the problem.  How are you handling the collision with the "ground" in your game?  What I generally do is check to see where the player WILL be and then adjust the position to place him directly on the ground.  The shaking is probably due to checking for collision after movement.  Below is a small excerpt from my Physics.class which checks for world collision BEFORE changing the position.  Keep in mind this is used for a tile based game so X and Y collisions are checked separately but it should give you the idea.

First allow movement such as left, right, up, down, jump etc:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public void moveLeft()
   {
      o.velocity.x -= moveSpeed;
      if(o.velocity.x < -maxSpeed) o.velocity.x = -maxSpeed;
   }
   
   public void moveRight()
   {
      o.velocity.x += moveSpeed;
      if(o.velocity.x > maxSpeed) o.velocity.x = maxSpeed;
   }


Next we apply gravity as a constant force:

1  
2  
3  
4  
5  
6  
7  
8  
9  
// Apply gravity if called for - note that o is a base class GameObject or whatever you want to call it.  
// GameObject has a Vector2 position, and another called velocity.  This allows us to attach the
// physics class to any GameObject.
     
      if(gravityEnabled)
      {
         o.velocity.y += gravity;
         if(o.velocity.y > maxGravity) o.velocity.y = maxGravity;
      }


Now we begin the checks for collision:

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  
// Test X axis for collision where the player WILL be if we allow movement
     
      if(o.velocity.x != 0)
      {
         destination.set(o.position.x + o.velocity.x , o.position.y, o.getWidth(), o.getHeight());
         
         if(overlap(destination))
         {
           
            if(o.velocity.x > 0)
            {
               o.velocity.x = 0;
               o.position.x = blockRect.x - o.getWidth();
            }
           
            else
            {
               o.velocity.x = 0;
               o.position.x = blockRect.x + blockRect.width;
            }
           
         }
      }
     
      // Test Y axis for collision where the player WILL be if we allow movement
           
      if(o.velocity.y != 0)
      {
// Creates a rectangle representing where the player will be - used for collision check
         destination.set(o.position.x, o.position.y + o.velocity.y, o.getWidth(), o.getHeight());
         
         if(overlap(destination))
         {
           
            if(o.velocity.y > 0)
            {
               o.velocity.y = 0;
               o.position.y = blockRect.y - o.getHeight();
            }
           
            else
            {
               o.velocity.y = 0;
               o.position.y = blockRect.y + blockRect.height;
            }
         }
      }


Notice that if there is an overlap in either axis I set the velocity for that axis to zero.  If no collision, then allow him to be positioned at the desired location. i.e.

1  
2  
3  
               // Finally apply adjusted forces
     
      o.position.add(o.velocity);


Also, I have gravity applied as a constant force so it doesn't depend on whether the player is falling, jumping or whatever.  This code works flawlessly no matter what direction the player is moving because collision is checked before ANY movement is allowed.  This prevents shaking behavior, getting stuck in walls etc. I hope this helps.
9  Game Development / Newbie & Debugging Questions / LibGDX - how to make a popup window on: 2014-12-05 15:43:04
Just like the title says, I want to have a "popup" window to display the players inventory in my game.  It looks like using a stage (or multiple stages) might accomplish this, but I am not using stages in my game.  I really don't want to have to redo the entire game engine so I am curious how this might be accomplished.

I would prefer something that would allow the level to be paused so to speak and the inventory window would then be displayed over the top of the level.  Any ideas?
10  Game Development / Game Play & Game Design / Re: Transfer an item from world to inventory on: 2014-12-04 13:35:23
Moogle thanks but what I was trying to avoid was having hundreds of Item objects in inventory because it's not very efficient.  I actually solved this by trimming the Item class down to a bare minimum removing all references to position, vecloity, etc and making an ItemContainer class derived from GameObject which has an Item as a class field.  

In addition, I'm using a ItemRegistry class which holds one instantiated Item of each type in an ObjectMap.  This allows me to destroy the ItemContainer after collision but retain a String key reference to the appropriate Item which then allows me to call the onUse() method.  God I love Java!

I can't tell you how many problems I've solved just by getting away from the computer and laying down at night.  The answers seem to come to me most of the time.  I'm marking this one solved.
11  Game Development / Game Play & Game Design / Transfer an item from world to inventory [SOLVED] on: 2014-12-03 23:05:41
Hi guys,

I am struggling a bit with my libgdx game design regarding how items should be handled with an inventory system.  Currently, my item class is based on a generic GameObject which has properties like position, velocity, and can interact with the world.

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 net.bigfootsoftware.seacraft.objects;

import net.bigfootsoftware.seacraft.engine.World;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;


public abstract class GameObject
{
   // FIELDS
     
   public Physics physics;
   
   protected World world;
   protected float width;
   protected float height;
   
   private String id;
   private Rectangle bounds;
   private boolean alive;
   
   public GameObject(String id, World world, float x, float y, float width, float height)
   {
      this.world = world;
      this.physics = new Physics(this);
      this.physics.position.set(x,y);
      this.id = id;
      this.width = width;
      this.height = height;
      this.bounds = new Rectangle();
      this.alive = true;
   }
   
   // GETTERS AND SETTERS
   
   public String getID()
   {
      return id;
   }
   
   public boolean isAlive()
   {
      return alive;
   }
   
   public void setAlive(boolean b)
   {
      alive = b;
   }
   
   public Rectangle getBounds()
   {
      bounds.set(physics.position.x, physics.position.y, width, height);
      return bounds;
   }
   
   public float getWidth()
   {
      return width;
   }
   
   public float getHeight()
   {
      return height;
   }
   
   public World getWorld()
   {
      return world;
   }

   
   // ABSTRACT METHODS
   
   public abstract void update(float dt);
   public abstract TextureRegion getTextureRegion();
   public abstract void collision(float dx, float dy);
   
   
   
}


My Item class looks like this.  Notice the abstract method onUse().

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  
package net.bigfootsoftware.seacraft.items;

import net.bigfootsoftware.seacraft.Game;
import net.bigfootsoftware.seacraft.engine.World;
import net.bigfootsoftware.seacraft.objects.GameObject;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;

public abstract class Item extends GameObject
{
   private TextureRegion region;
   
   public Item(String id, World world, float x, float y, float width, float height)
   {
      super(id,world,x,y,width,height);
      region = Game.res.getTextureRegion(id);
      physics.setJumpSpeed(-0.2f);
      physics.velocity.x = MathUtils.random(-0.05f, 0.05f);
      physics.jump();
   }

   
   public void update(float dt)
   {
      physics.apply(dt);
   }
   
   public abstract void onUse(float x, float y);


   @Override
   public TextureRegion getTextureRegion()
   {
      return region;
   }


   @Override
   public void collision(float dx, float dy)
   {
      physics.stopXVelocity();
   }
   
   
}


This works great because it allows each item to decide what should happen when it used.  The problem is how do I represent these items using a String id only as opposed to having hundreds of live Item objects in the players inventory?  I want to be able to remove the item from the world when collided with it, add that item as some sort of array of item id's, then select an item from inventory, and when "used" it will simply call the abstract method onUse().  

I thought about creating one static instance of each Item with some sort of Item registry but all GameObject requires a position and world reference making that impossible.  Do I just make an entirely new class to represent each item?  I might be over thinking this and I really could use some good advice.
12  Game Development / Newbie & Debugging Questions / Re: Smooth transition between angles on: 2014-03-10 03:03:14
Rest well sir.  What I have is working flawlessly for my little space shooter project.  Like I said, I haven't tried every angle yet, but it seems to work for what I need.  Marking this one solved.
13  Game Development / Newbie & Debugging Questions / Re: Smooth transition between angles on: 2014-03-10 02:42:23
Yeah the one thing I hate about libgdx.  Getting used to that fact that Y is up! 
14  Game Development / Newbie & Debugging Questions / Re: Smooth transition between angles on: 2014-03-10 02:28:37
After some playing around, I might have something that works.  At least for transitioning from 0 to 270 and then from 180 back to 270.  Haven't tried other angles yet but here it is for those that are interested.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public void doBehavior(GameObject g)
   {
      int currentDirection = g.getDirection();
       
      if(currentDirection == 0) currentDirection = 360;
     
      if(currentDirection < direction)
      {
         g.setDirection(currentDirection + 1);
      }
     
      else if(currentDirection > direction)
      {
         g.setDirection(currentDirection - 1);
      }
     
   }
15  Game Development / Newbie & Debugging Questions / Re: Smooth transition between angles on: 2014-03-10 02:23:49
Thanks for trying.  I guess I'll keep playing around and see what I can come up with.
16  Game Development / Newbie & Debugging Questions / Re: Smooth transition between angles on: 2014-03-10 02:10:09
Hmmm this makes him turn and go up.  I tried reversing the plus and minus but he just does a loop.  God I wish I'd studied trig in school...
17  Game Development / Newbie & Debugging Questions / Re: Smooth transition between angles on: 2014-03-10 02:04:23
In your last post you said: if diff1 < diff2 then turn left, else turn right (it could be opposite though ;P).  But what do you mean by turn left?  Do I add to the currentDirection?
18  Game Development / Newbie & Debugging Questions / Re: Smooth transition between angles on: 2014-03-10 02:00:23
I get what you're saying, but my movement is based on sin and cos, so how would this look in code?  The only way I can see is to either add or subtract from the currentDirection which works if he's moving left but not for moving right.  ?? 
19  Game Development / Newbie & Debugging Questions / Re: Smooth transition between angles on: 2014-03-10 01:54:51
Ok here is what I have so far:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
   public void doBehavior(GameObject g)
   {
      int currentDirection = g.getDirection();
      int diff1 = currentDirection - direction;
      int diff2 = direction - currentDirection;
     
      if(diff1 < 0) diff1 += 360;
      else if(diff1 > 360) diff1 -= 360;
   
      if(diff2 < 0) diff2 += 360;
      else if(diff2 > 360) diff2-= 360;
     
      // Now what do I do with these numbers?
     
   }
20  Game Development / Newbie & Debugging Questions / [Solved] Smooth transition between angles on: 2014-03-10 01:37:36
Hey guys,

I trying to create some simple AI for my enemy class.  I have an enemy moving 0 degrees initially, and I want to make him move smoothly to angle 270 (straight down in my coordinate system).  How can this be done?  

With my current code, he ends up "taking the long way around" so to speak but it works fine for transitioning from 180 to 270 (left to down).  I'm looking for something that will transition smoothly no matter what the chosen direction is and preferably so he would take the shortest route to get there.

This seems like it should be simple, but it's driving me nuts!  I am posting my code below which doesn't quite work.  In the below example, the variable direction is fixed at 270.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public void doBehavior(GameObject g)
   {
      int currentDirection = g.getDirection();
     
      // Smooth transition from current direction to new
     
     
      if(currentDirection < direction)
      {
         g.setDirection(currentDirection + 1);
      }
     
      else if(currentDirection > direction)
      {
         g.setDirection(currentDirection - 1);
      }
     
   }
21  Game Development / Newbie & Debugging Questions / Re: null objects in ArrayList on: 2014-02-23 04:14:44
@BurntPizza:  I have read that post and found it interesting.  My issue seemed to be due to a bad implementation of the Thread.sleep on windows platform (or so I've read) which caused the sleep time to be innacurate so I just wanted to see how the game would run from a steady timer "tick" if you will.  Also, I'm not using swing with the exception of JFrame I suppose.
22  Game Development / Newbie & Debugging Questions / Re: null objects in ArrayList on: 2014-02-23 04:09:56
@Drenius should I remove the Runnable interface altogether?  I'm actually more experienced with LWJGL than I am with Java 2D, but every tutorial/example code I've ever seen implements Runnable.
23  Game Development / Newbie & Debugging Questions / Re: null objects in ArrayList on: 2014-02-23 04:07:42
@opiop64: Well I'm assuming there is three based on the output of the stack trace.  The application itself, the GamePanel class, and the GameLoop class which as I mentioned is just an experiment and the bug occurred before I implemented that.
24  Game Development / Newbie & Debugging Questions / Re: null objects in ArrayList on: 2014-02-23 04:03:08
Thanks I appreciate your time.  The GamePanel is my engine which runs on it's own thread.  The GameLoop class is just a class which extends java.util.Timer and java.util.TimerTask to tell me when to update and redraw.  It doesn't do any heavy lifting and was just an experiment because I wasn't happy with the commented game loop code you see above.  I should mention that the bug occurred even without a third thread (i.e. the GameLoop class).

I have yet to find an "ideal" game loop that works for me without occasional lag, but that's another story.   Wink
25  Game Development / Newbie & Debugging Questions / Re: null objects in ArrayList on: 2014-02-23 03:55:00
It's pretty rare and random which is what makes it so annoying.  At one point I thought that it seemed to occur when there were many objects on the screen at once, but there's no definite pattern that I can find.  

EDIT: At no point in the game do I actually set any object to null, I only set it's alive boolean to false.  In my mind, this means that only the ObjectManager class will remove objects from the list which I guess is where garbage collection comes in.
26  Game Development / Newbie & Debugging Questions / Re: null objects in ArrayList on: 2014-02-23 03:41:54
Could this be some sort of multi threading issue?  I am posting my GamePanel class 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  
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  
package shooter.engine;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferStrategy;

import shooter.game.PlayScreen;

@SuppressWarnings("serial")
public class GamePanel extends Canvas implements Runnable, KeyListener, MouseMotionListener, MouseListener
{
   public static final String GAME_NAME = "SHOOTER v1.0";
   public static final int WIDTH = 1024;
   public static final int HEIGHT = 768;
   
   private Thread thread;
   private boolean running;
   
   private int FPS = 60;
   private int targetTime = 1000 / FPS;
   
   private Graphics2D g;
   private BufferStrategy bs;
   
   private ScreenManager scm;
   private GameLoop gameLoop;
   
   public GamePanel()
   {
      super();
      setPreferredSize(new Dimension(WIDTH, HEIGHT));
      setFocusable(true);
      requestFocus();
      setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
   }
   
   private void init()
   {
      running = true;
     
      createBufferStrategy(2);
      bs = getBufferStrategy();
      setIgnoreRepaint(true);
     
      Graphics2D gTemp = (Graphics2D) bs.getDrawGraphics();
     
      gTemp.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      gTemp.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
     
      scm = new ScreenManager();
      scm.changeScreen(new PlayScreen(scm));
     
      gameLoop = new GameLoop(targetTime);
     
      // Load game sounds
     
      SoundManager.add("laser");
      SoundManager.add("explosion");
      SoundManager.add("powerup");
      SoundManager.add("hit");
      SoundManager.add("powerup");
      SoundManager.add("boost");
     
   }
   
   @Override
   public void addNotify()
   {
      super.addNotify();
      addKeyListener(this);
      addMouseListener(this);
      addMouseMotionListener(this);
      init();
     
      // THREAD START
      if(thread == null)
      {
         thread = new Thread(this);
         thread.setDaemon(true);
         thread.start();
      }
   }

   @Override
   public void run()
   {
      /*long startTime;
      long urdTime;
      long waitTime;
         
      while(running)
      {
         startTime = System.nanoTime();
         
         update();
         draw();
         Toolkit.getDefaultToolkit().sync();
         
         urdTime = (System.nanoTime() - startTime) / 1000000;
         waitTime = (long) (targetTime - urdTime);
         
         if(waitTime < 0) waitTime = 5;
         try
         {
            Thread.sleep(waitTime);
         }
         
         catch(Exception e)
         {
            e.printStackTrace();
         }
      }*/

     
      while(running)
      {
         if(gameLoop.redraw())
         {
            update();
            draw();
            gameLoop.setRedraw(false);
         }
           
      }  
   }
   
   private void update()
   {
      scm.update();
   }
   
   private void draw()
   {
      g = (Graphics2D) bs.getDrawGraphics();
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
     
      // Clear the screen
     
      g.setColor(Color.BLACK);
      g.fillRect(0, 0, WIDTH, HEIGHT);
     
      // Draw game content here
     
      scm.draw(g);
     
      // Render to the screen
     
      g.dispose();
      bs.show();
   }
   
   @Override
   public void keyPressed(KeyEvent key)
   {
      int keyCode = key.getKeyCode();
      scm.keyPressed(keyCode);
   }

   @Override
   public void keyReleased(KeyEvent key)
   {
      int keyCode = key.getKeyCode();
      scm.keyReleased(keyCode);
   }

   @Override
   public void keyTyped(KeyEvent key)
   {
     
   }

   @Override
   public void mouseDragged(MouseEvent e)
   {
      scm.mouseMoved(e);
   }

   @Override
   public void mouseMoved(MouseEvent e)
   {
      scm.mouseMoved(e);
   }

   @Override
   public void mouseClicked(MouseEvent e)
   {
     
   }

   @Override
   public void mouseEntered(MouseEvent e)
   {
     
   }

   @Override
   public void mouseExited(MouseEvent e)
   {
     
   }

   @Override
   public void mousePressed(MouseEvent e)
   {
      scm.mousePressed(e);
   }

   @Override
   public void mouseReleased(MouseEvent e)
   {
     
   }
}
27  Game Development / Newbie & Debugging Questions / Re: null objects in ArrayList on: 2014-02-23 03:29:50
Any other ideas?  This is annoying bug that's slowing my progress on the game and I'm not even sure how to start debugging it.  Sometimes I can play through the game 10 times without incident and then suddenly the crash occurs. 
28  Game Development / Newbie & Debugging Questions / Re: null objects in ArrayList on: 2014-02-23 03:04:27
For my Enemy 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  
@Override
   public void destroy()
   {
      SoundManager.play("explosion");
      ObjectManager.add(new Explosion(x,y));
     
      // Type 4 and 5 enemies break into smaller types
     
      if(type == MAMA_YELLOW)
      {
         ObjectManager.add(new Enemy(x,y,BABY_YELLOW));
         ObjectManager.add(new Enemy(x,y,BABY_YELLOW));
         ObjectManager.add(new Enemy(x,y,BABY_YELLOW));
      }
     
      if(type == MAMA_MAGENTA)
      {
         for(int i=0; i<10; i++)
         {
            ObjectManager.add(new Enemy(x,y,BABY_MAGENTA));
         }
      }
     
      // Chance for powerup
     
      int chance = (int) (Math.random() * 100);
     
      if(chance < 5) ObjectManager.add(new PowerUp(x,y,PowerUp.EXTRA_LIFE));
      else if(chance < 20) ObjectManager.add(new PowerUp(x,y,PowerUp.SLOW_DOWN));
      else if(chance < 60) ObjectManager.add(new PowerUp(x,y,PowerUp.POWER));
     
   }


The destroy method was intended to allow one object to "change" into another.  Most of the logic for setting an object as "dead" occurs in the collision events.

EDIT:  @BurntPizza:  Thanks for the info on i--.  I've often wondered why people do this.  This doesn't seem to be the cause of the problem though as the crash occurs with or without it.
29  Game Development / Newbie & Debugging Questions / Re: null objects in ArrayList on: 2014-02-23 02:55:28
I'm not.  I'm only adding with the new operator.   Huh
30  Game Development / Newbie & Debugging Questions / null objects in ArrayList on: 2014-02-23 02:31:56
Hey guys,

I am getting an occasional null pointer exception in my games ObjectManager class.  I can't seem to find a pattern as to why this is happening and could use some help.  Every object in my game is added and removed by this ObjectManager class by checking the objects alive property.  This property is set in collision methods when two objects collide so that the ObjectManager knows when to remove them.

Below is the ObjectManager update method:
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  
public void update()
   {
      GameObject a, b;
     
      // Update the objects and cull the dead
     
      for(int i=0; i<objects.size(); i++)
      {
     
         objects.get(i).update();
         
         if(objects.get(i).isAlive() == false)
         {
            objects.get(i).destroy();
            objects.remove(i);
     
         }
         
      }
     
      // Check for collision
     
      for(int i=0; i<objects.size(); i++)
      {
         for(int j=i; j<objects.size(); ++j)
         {
            a = objects.get(i);
            b = objects.get(j);
           
            if(a == b) continue;
           
            // Finally do the test and alert both objects
           
            if(collision(a,b))
            {
               a.collision(b);
               b.collision(a);
            }
           
           
         }
         
      }
     
     
   }


Here is my relevant stack trace:
1  
2  
3  
4  
5  
6  
7  
8  
Exception in thread "Thread-3" java.lang.NullPointerException
   at shooter.engine.ObjectManager.collision(ObjectManager.java:110)
   at shooter.engine.ObjectManager.update(ObjectManager.java:83)
   at shooter.game.PlayScreen.update(PlayScreen.java:33)
   at shooter.engine.ScreenManager.update(ScreenManager.java:15)
   at shooter.engine.GamePanel.update(GamePanel.java:137)
   at shooter.engine.GamePanel.run(GamePanel.java:127)
   at java.lang.Thread.run(Unknown Source)


Here is the collision method from the stack trace:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
   private boolean collision(GameObject a, GameObject b)
   {
         
      double dx = a.getX() - b.getX();
      double dy = a.getY() - b.getY();
     
      double dist = Math.sqrt(dx * dx + dy * dy);
      if(dist < a.getR() + b.getR()) return true;
     
      return false;
   }


The line that it's pointing to is in the collision method.  I don't see how it could be null because "dead" objects are removed before checking for collision.  The crash doesn't always occur in the collision method, sometimes it occurs in the update method posted above.  Any ideas?  I would be happy to post more code if needed.
Pages: [1] 2
 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (48 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (103 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!