Вопрос

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

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top