防止用户在半空中跳跃
Prevent user from jumping in mid-air
我正试图阻止精灵在半空中再次跳跃。但是,我实现它的方式是错误的,我真的迷路了。
-(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 的新手。
实际上,CCActionDelay 可用于在一系列操作中创建暂停,但在这种情况下并不是很有用。相反,我会管理触摸开始时的 'jumping' 状态,而不用担心 touchEnded,就像这样。
你需要一个属性来控制这个状态:
@属性(非原子,读写) 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 : 未编译、测试,仅供参考。有不止一种方法可以做到这一点:)
我正试图阻止精灵在半空中再次跳跃。但是,我实现它的方式是错误的,我真的迷路了。
-(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 的新手。
实际上,CCActionDelay 可用于在一系列操作中创建暂停,但在这种情况下并不是很有用。相反,我会管理触摸开始时的 'jumping' 状态,而不用担心 touchEnded,就像这样。
你需要一个属性来控制这个状态:
@属性(非原子,读写) 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 : 未编译、测试,仅供参考。有不止一种方法可以做到这一点:)