执行操作时禁用触摸

时间:2014-04-23 15:08:25

标签: ios cocos2d-iphone

我无法在执行操作时暂时禁用触摸交互。当我向上滑动时,我的角色正在跳跃,但是如果你再次向上滑动,它会再次翻转。我想在执行操作时禁用任何交互。

我有以下代码叫做"开始"假设暂时禁用触摸的功能,以及"结束"在动作完成后应该重新启用触摸交互的功能,所有功能都在ccactionspawn中,但它不起作用。

-(void)begin
{
[[UIApplication sharedApplication]beginIgnoringInteractionEvents];
}

-(void)end
{
[[UIApplication sharedApplication]endIgnoringInteractionEvents];

}


-(void)jump
{
CCActionRotateBy* flip = [CCActionRotateBy actionWithDuration:0.8f angle:360];
CCActionJumpBy* jump = [CCActionJumpBy actionWithDuration:0.8f position:CGPointMake(150, 0) height:130 jumps:1];
CCAction *actionMove = [CCActionMoveTo actionWithDuration:1.0f position:CGPointMake(-_character.contentSize.width/2, self.contentSize.height/4)];
CCActionSpawn *flipJump = [CCActionSpawn actions:[CCActionCallFunc actionWithTarget:self selector:@selector(begin)], jump, flip, actionMove, [CCActionCallFunc actionWithTarget:self selector:@selector(end)], nil];
[_character runAction:flipJump];
}

感谢您的帮助!

- 编辑工作代码。
感谢所有帮助人员!如果有人需要,这是我的工作代码:

-(void)begin
{
self.userInteractionEnabled = FALSE;
}

-(void)end
{
self.userInteractionEnabled = TRUE;
}


-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLoc1 = [touch locationInNode:self];
firstTouch = touchLoc1;
CCLOG(@"touched");
}

-(void) touchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLoc2 = [touch locationInNode:self];
lastTouch = touchLoc2;

//Minimum length of the swipe
float swipeLength = ccpDistance(firstTouch, lastTouch);

//Check if the swipe is an up swipe and long enough
if (firstTouch.y < lastTouch.y && swipeLength > 10) {
        CCActionRotateBy* flip = [CCActionRotateBy actionWithDuration:0.8f angle:360];
        CCActionJumpBy* jump = [CCActionJumpBy actionWithDuration:0.8f position:CGPointMake(150, 0) height:130 jumps:1];
        CCAction *actionMove = [CCActionMoveTo actionWithDuration:1.0f position:CGPointMake(-_character.contentSize.width/2, self.contentSize.height/4)];
        CCActionSequence *flipJump = [CCActionSequence actions:[CCActionCallFunc actionWithTarget:self selector:@selector(begin)], flip, actionMove, [CCActionCallFunc actionWithTarget:self selector:@selector(end)], nil];
        [_character runAction:jump];
    [_character runAction:flipJump];
    }
}

3 个答案:

答案 0 :(得分:0)

请勿禁用触摸,只需保留状态变量并检查是否正在翻转,如果您已经翻转,请不要翻转。

编辑编写代码......

-(void)begin
{
}

-(void)end
{
    isFlipping = NO;
}


-(void)jump
{
    if (!isFlipping) {
        isFlipping = YES;
        CCActionRotateBy* flip = [CCActionRotateBy actionWithDuration:0.8f angle:360];
        CCActionJumpBy* jump = [CCActionJumpBy actionWithDuration:0.8f position:CGPointMake(150, 0) height:130 jumps:1];
        CCAction *actionMove = [CCActionMoveTo actionWithDuration:1.0f position:CGPointMake(-_fufunaken.contentSize.width/2, self.contentSize.height/4)];
        CCActionSpawn *flipJump = [CCActionSpawn actions:[CCActionCallFunc actionWithTarget:self selector:@selector(begin)], jump, flip, actionMove, [CCActionCallFunc actionWithTarget:self selector:@selector(end)], nil];
        [_character runAction:flipJump];
    }
}

isFlipping将是一个BOOL,可能在您的视图控制器中。您可能希望将跳转体包装在@synchronized(self)块中以避免线程问题,但实际上这可能会使事情变得过于缓慢。如果他翻了两次,这真的很重要吗?

答案 1 :(得分:0)

试试这个:

-(void)begin
{
 self.userInteractionEnabled = FALSE;
}

-(void)end
{
 self.userInteractionEnabled = TRUE;
}

答案 2 :(得分:0)

CCActionSpawn一次又一次地执行所有这些操作。这意味着您不会等到动画完成。不要使用CCActionSpawn,而应使用CCActionSequence来执行彼此之后的所有操作。

创建序列应如下所示:

CCActionSequence *flipJump = [[CCActionSequence actions:[CCActionCallFunc actionWithTarget:self selector:@selector(begin)], jump, flip, actionMove, [CCActionCallFunc actionWithTarget:self selector:@selector(end)], nil];

此外,您需要在userInteractionEnabledTRUE方法中将FALSE设置为beginend,或者维护另一个标志并在其中使用该标志应该处理或不处理触摸触摸的触摸方法。

第一个选项的示例:

-(void)begin
{
 self.userInteractionEnabled = FALSE;
}

-(void)end
{
 self.userInteractionEnabled = TRUE;
}