Reputation: 55
I am attempting to make it so in my CCScene, if a user taps to shoot a projectile there's a delay at the end of the bullet shot from allowing them to shoot another, maybe 2 seconds.
I've attempted this:
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CCSprite *arrow = [CCSprite spriteWithImageNamed:@"arrow.png"];
arrow.position = player.position;
[self addChild:arrow];
CCActionMoveTo *actionStart = [CCActionMoveTo actionWithDuration:1.3f position:targetPosition];
CCActionRemove *actionEnd = [CCActionRemove action];
CCActionDelay *delay = [CCActionDelay actionWithDuration:2.0];
[arrow runAction:[CCActionSequence actions: actionStart, actionEnd, delay, nil]];
}
but I am still able to repeatedly click to fire projectiles with no delay. Any ideas how I could fix this, and more importantly what I'm doing wrong?
Upvotes: 0
Views: 119
Reputation: 1042
Why are you creating a new arrow each time? You can instead just do this:
Create an arrow property:
@property (nonatomic, strong) CCSprite* arrow;
@property (nonatomic, strong) CCSprite* player;
@property (nonatomic, assign) CGPoint targetPosition;
Create the arrow sprite:
- (void)onEnter
{
[super onEnter];
self.player = ...
self.targetPosition = ...
self.arrow = [CCSprite spriteWithImageNamed:@"arrow.png"];
self.arrow.position = self.player.position;
self.arrow.visible = FALSE;
[self addChild:self.arrow];
}
Then in touches began:
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
if (!self.arrow.numberOfRunningActions)
{
CCActionMoveTo* move = [CCActionMoveTo actionWithDuration:1.3f
position:self.targetPosition];
CCActionShow* show = [CCActionShow action];
CCActionRemove* hide = [CCActionHide action];
CCActionDelay* delay = [CCActionDelay actionWithDuration:2.0];
CCActionSequence* seq = [CCActionSequence actions:show, move, hide, delay, nil];
[self.arrow runAction:seq];
}
}
Hope this helped.
Upvotes: 1
Reputation: 3012
It's not working because the action you applied applies to your arrow CCSprite
.
This means that the delay is in effect but on the arrow
sprite that you created before, effectively having no effect.
I think for this use case you might just have a NSDate
property called something like lastShootingTime
and in touchBegan
test the time and if the timeElapsedSinceNow
is larger than what you want start the new action sequence and reset the lastShootingTime
.
Upvotes: 0