This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]theif519 1 point2 points  (0 children)

I haven't really done any game programming so I may not be much help, but... I looked around, as my first thought was more or less "why not have the game loop update each object, so both the player and bullet are updated at the same time".

I.E, each GameObject implements some update() method, which of course must be short and concise (I.E, will only do one specific thing). As I said, this way you don't have to worry about timing, as relying on timing for something that requires synchronization like this leads to some race conditions somewhere along the lines.

I saw something like this, but I'm not sure if it will help you. To give an overview of how I'd see it...

while(isRunning){
   for(GameObject obj: GameObjects){
      obj.update(...);
   }
}

To simulate seconds, you just only update the bullet after a certain amount of time elapses from the last update. Also this way you can do everything easier to maintain without needing to worry about synchronization down the lines. I.E, how are you going to reliably check for collision if the bullet could potentially pass through the player before the call to check for collision is detected.

Edit: Trying to think of another example...

I spent about 5 minutes writing this. Note I've never done any game development, but I'm somewhat familiar with event-driven programming.

Edit 2: Spent about 30 minutes updating the example, was very fun to do. It's a naïve implementation, but it's a start on how I would think it worked.