0

Ok, I have this main class called Enemy, and inside it I have subclasses of different enemies (ie ZombieEnemy). I need a way to target all sprites/subclasses of Enemy. Ie, for collision detection, I need a way to see if ALL Enemy's are 'dead' to end the Level.

Thanks

Joethemonkey101
  • 149
  • 2
  • 18

1 Answers1

1

There are plenty of ways to do this. One is to add a method to your Enemy class like -(BOOL)isEnemy that simply returns YES. (That'd actually be more useful if Enemy has a superclass that you can customize, like GameObject. Implement -isEnemy in that class to return NO. Otherwise, you won't know if you can call -isEnemy on a given object.) Subclasses will automatically inherit this method. Alternately, you could test the class of each object using -isKindOfClass:. Or, since you're the one creating enemies, you could certainly keep a list of all active enemies. This is probably the best plan if you have lots of objects on the screen, only some of which are Enemy objects.

Deciding when all enemies are dead is something you probably want to do very often. It might make sense to keep a list of live enemies. When an enemy dies, remove it from the list. You can quickly test whether the player has successfully cleared the level by checking the length of the live enemies list. If it's greater than 0, there's more work to do.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • In any given Level, I have a few (2-4) different Enemy's on the screen (just as an example). I just want to get all Enemy's (every type, and each type is a different subclass), and use them. Like I said, I need to make an if method to end the Level. (If all Enemy's are killed, blah blah blah). Of course, that's what I want to do in English ;) I'd like to know which way is the most efficient of basically saying "all Enemy's". Thanks – Joethemonkey101 Mar 23 '11 at 20:03
  • Oh and thanks for the info on the 'list', but I've got that solved, in a sense. I have an 'allEnemiesKilled' method. The problem is that I don't know how to check if ALL enemies really are killed. For example, if I wanted to see if all of the ZombieEnemy's are killed, I would do (in English _and_ code) if all ZombieEnemy's.hp = 0, end Level. Tell me if I'm not explaining clearly enough, I know I can be confusing sometimes :) – Joethemonkey101 Mar 23 '11 at 20:06
  • Basically to summarize, what can replace "all ZombieEnemy's"? I want to make all sprites of that class into a group, so I could call `if(allZombieEnemies.hp = 0) { //end Level }` – Joethemonkey101 Mar 23 '11 at 20:08
  • How could an enemy make it onto the 'allEnemiesKilled' list if it's not really killed? Can some enemies be only "mostly dead"? Is this game based on _The Princess Bride_? ;-) – Caleb Mar 23 '11 at 20:11
  • No haha dead enemies = dead list. I'm confused on how to 'abbreviate' all ZombieEnemy's into allZombieEnemies (as an instance) basically. – Joethemonkey101 Mar 23 '11 at 20:45
  • Do you have a way to do that? – Joethemonkey101 Mar 24 '11 at 19:29
  • Still need help with what, specifically? If you just need to determine whether all the enemies are dead, compare the size of the dead enemies list to the number of enemies that you started with. Or, if you have a live enemies list, look at the number of objects in it. If zero, you're done. If there's more to it than that, consider editing your question so that we know what you're talking about. – Caleb Mar 31 '11 at 00:16
  • It's not that at all. I want to know how to turn "All sprites from this class" into code. So basically I could call "if all sprites from Enemy class".hp = 0, end level. I don't know quite how to do that – Joethemonkey101 Apr 01 '11 at 22:26