防止用户在半空中跳跃

时间:2015-02-11 19:19:26

标签: objective-c cocos2d-iphone

我试图阻止精灵在半空中再次跳跃。但是,我实施它的方式是错误的,我真的输了。

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
   CCActionJumpBy *jump_up = [CCActionJumpBy actionWithDuration:1.0f position:ccp(0, 100) height:50 jumps:1];
   [player runAction:jump_up];
   CCActionJumpBy *come_down = [CCActionJumpBy actionWithDuration:1.0f position:ccp(0, -100) height:50 jumps:1];
  [player runAction:come_down];

}

-(void) touchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    CCActionDelay *delay = [CCActionDelay actionWithDuration:2.0];
    [player runAction:delay];
}

根据我的理解,延迟应该阻止用户再次跳跃。有小费吗?对不起,我是对objective-c的新手。

1 个答案:

答案 0 :(得分:1)

实际上,CCActionDelay对于在一系列动作中创建暂停很有用,在此上下文中并不真正有用。相反,我会管理跳跃'触摸状态开始了,而不是担心touchEnded,就像这样。

您将需要一个属性来控制此状态:

@property(nonatomic,readwrite)BOOL isPlayerJumping;

并将touchBegan更改为:

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
   if (!self.isPlayerJumping) {
       CCActionJumpBy *jump_up = [CCActionJumpBy actionWithDuration:1.0f position:ccp(0, 100) height:50 jumps:1];
       CCActionJumpBy *come_down = [CCActionJumpBy actionWithDuration:1.0f position:ccp(0, -100) height:50 jumps:1];
       CCActionCallBlock *end_jump = [CCCallBlock actionWithBlock:^{
          self.isPlayerJumping = NO;
       }];
       CCActionbSequence *seq = [CCActionSequence actions:jump_up,come_down,end_jump,nil];
       [player runAction:seq];
       self.isPlayerJumping = YES;
    }
}

}

obcit:没有编译,测试,只是为了它的想法。有多种方法可以做到这一点:)