Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //In Java, you need to use an iterator if you plan to remove objects from a list as you cycle through it
- //The <Brackets> are a template - this means it's an Iterator that only has Entities.
- //"enemies" is a LinkedList<Entity> I have defined elsewhere. Yes, it's a linked list of only Entities.
- Iterator<Entity> enemyIterator = enemies.iterator();
- while(enemyIterator.hasNext())
- {
- //Grab the next entity
- Entity entity = enemyIterator.next();
- //Have it do its step
- entity.doStep();
- //Check collisions
- //This is a Java for-each loop, it pulls each Entity from playerBullets and stores it in "bullet"
- //I can use it here instead of an iterator because I'm not removing from playerBullets in this loop
- for(Entity bullet : playerBullets)
- {
- if(entity.checkCollision(bullet))
- {
- //It's always easiest to check collisions from one entity, then handle collisions in both directions
- entity.handleCollision(bullet);
- bullet.handleCollision(entity);
- }
- }
- //Check death
- if(entity.isDead())
- {
- //Removes the object from the list
- enemyIterator.remove();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement