【问题标题】:Disable touch while performing action执行操作时禁用触摸
【发布时间】:2014-04-23 15:08:24
【问题描述】:

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

我有以下代码调用“开始”函数,假设暂时禁用触摸,以及在动作完成后重新启用触摸交互的“结束”函数,所有这些都在 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];
    }
}

【问题讨论】:

    标签: ios cocos2d-iphone


    【解决方案1】:

    CCActionSpawn 同时并行执行所有这些操作。这意味着您无需等到动画完成。而不是使用CCActionSpawn,您应该使用CCActionSequence,它会依次执行所有操作。

    创建序列应如下所示:

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

    此外,您需要将userInteractionEnabled 设置为TRUEFALSE int beginend 方法或维护另一个标志并在触摸方法中使用该标志来检查是否应该处理触摸或不是。

    第一个选项的示例:

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

    【讨论】:

    • 感谢您的帮助!我使用 spawn 而不是 sequence 的原因是因为我想让跳跃和翻转同时发生。在您的帮助下,我对其进行了一些修改,先发生跳跃,然后使用序列进行翻转和结束和开始方法。
    【解决方案2】:

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

    编辑编写代码...

    -(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) 块中以避免线程问题,但实际上这可能会使事情变得太慢。如果他翻转两次,那真的很重要吗?

    【讨论】:

    • 编辑答案以编写代码。如果您觉得它有帮助,请通过单击复选标记将其标记为已接受的答案。谢谢!
    • 首先,感谢您的帮助,非常感谢。我尝试了该代码,但同样的事情正在发生,每次滑动它都会继续跳跃。多次跳跃的问题在于,当多次跳跃确实发生时,我的角色会飞离屏幕或颠倒过来。
    【解决方案3】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-30
      • 1970-01-01
      相关资源
      最近更新 更多