带有touchBegan Cocos2d iOS的延迟



我试图在我的CCScene中做到这一点,如果用户点击射击炮弹,在子弹射击结束时会有延迟,允许他们射击另一个,可能是2秒。

I've attempt 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]];
}

但我仍然可以重复点击发射炮弹,没有延迟。你知道我该怎么解决这个问题吗,更重要的是,我做错了什么?

为什么每次都要创建一个新箭头?你可以这样做:

创建箭头属性:

@property (nonatomic, strong) CCSprite* arrow;
@property (nonatomic, strong) CCSprite* player;
@property (nonatomic, assign) CGPoint targetPosition;
创建箭头精灵:
- (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 contacts 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];
    }
}

希望有帮助。

它不工作,因为您应用的操作适用于您的箭头CCSprite

这意味着延迟是有效的,但在你之前创建的arrow精灵上,实际上没有影响。

我认为对于这个用例,你可能只是有一个NSDate属性称为lastShootingTimetouchBegan测试时间,如果timeElapsedSinceNow大于你想要的开始新的动作序列和重置lastShootingTime

最新更新