Reputation: 47
This is my power up button:
CCMenuItemImage *bottone = [CCMenuItemImage itemFromNormalImage:@"Icon-72.png" selectedImage:@"Icon-72.png" target:self selector:@selector(bottone)];
bottone.position = ccp(200,-100);
CCMenu *menu = [CCMenu menuWithItems:bottone, nil];
[self addChild:menu];
And this is the method when I touch the button:
-(void) bottone
{
float scaleDuration = 1.0;
float waitDuration = 5.0;
_invincible = YES;
CCParticleSystemQuad *boostEffect = [_boostEffects nextParticleSystem];
[boostEffect resetSystem];
[_ship runAction:
[CCSequence actions:
// [CCMoveBy actionWithDuration:scaleDuration position:ccp(winSize.width * 0.6, 0)],
[CCDelayTime actionWithDuration:waitDuration],
// [CCMoveBy actionWithDuration:scaleDuration position:ccp(-winSize.width * 0.6, 0)],
nil]];
[self runAction:
[CCSequence actions:
// [CCScaleTo actionWithDuration:scaleDuration scale:0.75],
[CCDelayTime actionWithDuration:waitDuration],
[CCScaleTo actionWithDuration:scaleDuration scale:1.0],
[CCCallFunc actionWithTarget:self selector:@selector(boostDone)],
nil]];
}
It works but I want that the player can use power up X times. Explain better, touch the button 1 or 2 times and stop, the button becomes unusable.
Upvotes: 0
Views: 166
Reputation: 306
Make your menu an instance variable, and then create a counter to check for number of times it has been used as LearnCocos2D suggested. and define a max number of uses i.e.
// In your .h
int _numUses = 0;
CCMenu *menu;
// In your .m
#define kMaxUses 2
You can then handle the restriction in a few ways. First you can check after the button is pressed to tell if it is available using the
if(_numUses < kMaxUses) {
// do use
}
I personally do not like this approach since it leaves the user still able to press the button without any information that it wont work. The user may then think that something is broken in the game. What I would do, is at the end of the action of your button method (after you increment the _numUses) disable the button. To do this, when you create the button add a tag value to it i.e.
CCMenuItemImage *bottone = [CCMenuItemImage itemFromNormalImage:@"Icon-72.png" selectedImage:@"Icon-72.png" target:self selector:@selector(bottone)];
bottone.position = ccp(200,-100);
buttone.tag = 100;
menu = [CCMenu menuWithItems:bottone, nil];
[self addChild:menu];
Then at the end of your button method, add this
_numUses += 1;
if(_numUses > kMaxUses) {
CCMenuItemImage *buttone = (CCMenuItemImage *)[menu getChildByTag:100];
[buttone setIsEnabled:NO];
}
That way when the player can no longer use the button, it is disabled and obvious to them that they cannot use it
Upvotes: 1