0
\$\begingroup\$

I'm new to game development. I do have basic knowledge in Java and also have moderately good experience in OOP from other languages.

I started developing this simple 2D game as my first project.

The game is simple, I have a character, I can move only right and left with constant speed.

I want to add obstacle objects that fall from the sky (top) that I have to dodge. I created the main class (StartingClass) that contains the game loop. receives input from the user's keyboard and moves the character accordingly.

I created a Character class which describes the character position, and reflect its position with painting its image.

Everything is well. However, I'm now trying to add the falling objects. My delima is how to implement them.

I started by creating a new class (FallingObject). In the instructor I create its width, height, falling speed.. etc (randomly). The issue I'm having is to determine how to update this object's position. I would have created an update method for the object and call it every time in the game loop to update its position. But my problem is I will have multiple objects falling simultaneously, each with different speed. So these are the solutions that came to my mind:

  • Visible/Active falling objects will be stored in an array list *

1) on every game loop, iterate over the falling objects and update them individually, which doesn't sound that friendly to me.

2) In the falling object instructor, when finished assigning the random values, call a synchronized "startFalling" method on the object, so it keeps falling until it hits the ground.

3) My FallingObject class would implement the runnable interface, and run the function described above through thread. But still... thread for each falling object? I'm not liking that idea.

So I would like to know if I'm somewhat in the right direction in my way of thinking to solve this issue, and which of the above suggestions you think is best, or if there's way better solutions (which I believe is true) that I'm unable to see due to my limited experience in game development design / java.

Thank you very much for your help and support! Cheers Shady

\$\endgroup\$
1
\$\begingroup\$

I think you are over thinking this. Just have each object has its own update method that takes a delta time; then do

 obj.vy += 0.01 * dt; //gravity 
 obj.y += obj.vy * dt; 

And call this for every object in each game step and your done!

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ That's the one I intend to go with. Basically having an array in my starting class. Populate it with falling objects (and remove them when they hit the ground), and on every game loop, iterate over these objects (the array) and update their positions with their update method. That's what you meant yes? \$\endgroup\$ – shadyhossin Dec 4 '14 at 22:50
  • \$\begingroup\$ I'm new to game development, and java, so yeah I'm doubting myself, and trying to make sure I'm using best (up to date) practices and designs. For some reason, iterating over all objects in the array (60 times per second) made me feel somewhat uncomfortable. but I guess that's the standard way of doing it. \$\endgroup\$ – shadyhossin Dec 4 '14 at 22:51
  • 1
    \$\begingroup\$ Yep, if you want objects to continue falling, instead of deleting the object that hit the ground, change its y to the sky again and set its x to a random value. There is no way for you to avoid not iterating over all objects if they have a different position in every game step and theres no patterns to exploit. \$\endgroup\$ – user55564 Dec 4 '14 at 22:55
  • \$\begingroup\$ You dont need to (re)populate the array in every game step, just have one array that contains all the falling objects. Make sure that if you are doing actual removal of objects, update the index variable accordingly so you dont skip over objects. \$\endgroup\$ – user55564 Dec 4 '14 at 23:02
  • 1
    \$\begingroup\$ As a general rule of thumb, get it working first. After you have it working, worry about a second iteration when either it is slow or you have time later to refactor. \$\endgroup\$ – Paul Renton Dec 5 '14 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.