In my application one player and 10 targets are there. Each target appears one after the other (from target1 to target10). It's a shooting game. If we hit the first target then second target will come. The targets have properties like name, speedOfGunDraw, probability to hit the player, speedOfFire. What should I do to make them appear one after the other with these properties. I am using CCMenuItem for the target. I am using a sprite for the player. Please give me idea to do this. Thank You.
views:
90answers:
1How can I add sprite image from a set of sprites which have different properties for each sprite?
To address your question: With Cocos2D, your scene would create the sprites. You can get the current running scene and send it a message ("I'm shot", for instance). This can be done through the director.
[[CCDirector sharedDirector] runningScene]; // returns a pointer to the running scene
[[[CCDirector sharedDirector] runningScene] someoneShotMe: self]; // will message the scene that you're shot.
Alternatively, if your scene is not controlling things, set the object your wish to be informed as a delegate, upon creation of the "enemy".
Enemy * enemy1 = [[Enemy alloc] init];
[enemy1 setDelegate: self];
// and then from your enemy object, you call a message on the delegate
[self->delegate someoneShotMe: self];
I think you're over complicating this because you don't use MVC in there.
You should not subclass sprites to give them more functionality beyond the "view".
Properties like probabilityToHitPlayer
don't affect the view directly, so shouldn't be stored in a sprite.
Create a new class, like Enemy
(subclass of NSObject
), which contains a sprite, along with other properties like probabilityToHitPlayer
Enemy can then handle the logic (it is a controller) while Sprite handles the visible parts.
Also, using menu items because they have touch detection? Not pretty. Instead, look into CCTargetedTouchDelegate.