Apart from the shading update, the rainbow crystals and the washing machines, I thought I’d write out the update loop.

You’ll notice that there’s a few steps where addition and removal of objects are queued. This is to get around concurrent modification exceptions, but it also allows me to clean up nicely, all in one place.

A contact listener was created to hold a set of objects of a certain type that are have collided with the user’s sensor. I’m still learning about Box2D sensors, and sometimes it doesn’t work until I nudge the object, but in general it does the job.

Update loop:

  • If the level hasn’t been generated:
    • If the level isn’t currently being generated:
      • Start generating it!
  • If the level is ready:
    • Move the player body (Box2D)
    • If the player performed an action/hit:
      • Loop through everything in range, and call hit method on the game item
    • Remove all the bodies (Box2D) that have been queued for removal
    • Step the world physics (Box2D)
    • Loop through each item
      • Update item (given the time delta and the current game state)
    • Remove dead items, queued for removal
    • Add new items, queued for adding

I’m not a big fan of giving the game state to each item as they’re updated, so I might reduce visibility using an interface.

Leave a comment

Your email address will not be published. Required fields are marked *