推迟使用touchBegan Cocos2d iOS

时间:2014-06-05 05:40:28

标签: ios objective-c cocos2d-iphone

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

我试过这个:

- (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]];
}

但是我仍然可以反复点击以毫不拖延地发射射弹。我有什么想法可以解决这个问题,更重要的是我做错了什么?

2 个答案:

答案 0 :(得分:1)

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

创建一个箭头属性:

@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];
}

然后开始接触:

- (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];
    }
}

希望这会有所帮助。

答案 1 :(得分:0)

它无效,因为您应用的操作适用于您的箭头CCSprite

这意味着延迟在您之前创建的arrow精灵上生效,实际上没有效果。

我认为对于这个用例,您可能只有一个名为NSDate的{​​{1}}属性,并且在lastShootingTime中测试时间以及touchBegan是否大于您想要启动新的操作序列并重置timeElapsedSinceNow