Advertisement
Guest User

SquashMonster's games programming - lesson 2, example code b

a guest
Oct 5th, 2011
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. //In Java, you need to use an iterator if you plan to remove objects from a list as you cycle through it
  2. //The <Brackets> are a template - this means it's an Iterator that only has Entities.
  3. //"enemies" is a LinkedList<Entity> I have defined elsewhere. Yes, it's a linked list of only Entities.
  4. Iterator<Entity> enemyIterator = enemies.iterator();
  5. while(enemyIterator.hasNext())
  6. {
  7. //Grab the next entity
  8. Entity entity = enemyIterator.next();
  9. //Have it do its step
  10. entity.doStep();
  11. //Check collisions
  12. //This is a Java for-each loop, it pulls each Entity from playerBullets and stores it in "bullet"
  13. //I can use it here instead of an iterator because I'm not removing from playerBullets in this loop
  14. for(Entity bullet : playerBullets)
  15. {
  16. if(entity.checkCollision(bullet))
  17. {
  18. //It's always easiest to check collisions from one entity, then handle collisions in both directions
  19. entity.handleCollision(bullet);
  20. bullet.handleCollision(entity);
  21. }
  22. }
  23. //Check death
  24. if(entity.isDead())
  25. {
  26. //Removes the object from the list
  27. enemyIterator.remove();
  28. }
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement