【问题标题】:iOS Sprite Kit how to disable user touches for the scene while animation is playing?iOS Sprite Kit 如何在播放动画时禁用用户对场景的触摸?
【发布时间】:2013-12-14 19:45:04
【问题描述】:

我有一个回合制游戏,对象在游戏板上移动。这个动作需要时间,每次移动大约需要 0.4 秒。 我想禁用对场景的进一步触摸,直到我的对象到达目的地,然后我可以为新位置执行计算。

我尝试使用布尔动画标志,但似乎它们要么被忽略,要么不可靠。

[selectedModel runAction:[SKAction moveTo:[MapOfTiles positionForTileCoord:s.position] duration:0.75] completion:^{
    [self animateNextMove];
    if(reachedDestination)
    {
        isAnimatingMove = NO;
    }
}];

我注意到在用户交互设置为 NO 的情况下覆盖透明 SpriteNode 似乎仍然可以捕获触摸(我计划添加一个透明覆盖并设置它的用户交互打开/关闭以捕获触摸。

在 Sprite Kit 执行一些动画时禁用节点上的按钮和触摸的最佳方法是什么?

【问题讨论】:

    标签: ios objective-c animation sprite-kit user-interaction


    【解决方案1】:

    在你的场景中做[self setUserInteractionEnabled:NO];

    [selectedModel runAction:[SKAction moveTo:[MapOfTiles positionForTileCoord:s.position] duration:0.75] completion:^{
        [self animateNextMove];
        [self setUserInteractionEnabled:NO];
        if(reachedDestination)
        {
            [self setUserInteractionEnabled:YES];
        }
    }];
    

    reachedDestination 变为真之前,这将阻止对您的场景进行所有接触...这就是您要找的吗?

    【讨论】:

    • 我都试过了 self.userInteractionEnabled = NO; self.view.userInteractionEnabled = 否;两者仍然允许所有基于触摸的代码触发。
    【解决方案2】:

    这似乎有效:

    cover是一个实例变量节点,添加到场景中时,是不允许touch通过的。您可能需要调整场景的大小,我的是风景。

    -(void)preventUserInteraction:(BOOL)prevent
    {
        if(cover == nil)
        {
            cover = [[SKSpriteNode alloc] initWithColor:[UIColor clearColor] size:CGSizeMake(1024,768 )];
            cover.position = CGPointMake(1024/2, 768/2);
        }
        if(prevent)
        {
            [self addChild:cover];
        }else
        {
            [cover removeFromParent];
        }
    }
    

    【讨论】:

      【解决方案3】:

      我是这样做的:

      //Disable user interaction
      [self setUserInteractionEnabled: NO];
      
      // Grab our hero sprite node, create an image atlas of textures and push into an action
      SKSpriteNode *hero = (SKSpriteNode *)[self childNodeWithName: @"hero"];
      
      SKTextureAtlas *heroPunchAtlas = [SKTextureAtlas atlasNamed: @"heroPunch"];
      
      SKTexture *punchF1 = [heroPunchAtlas textureNamed: @"heroPunchF1"];
      SKTexture *punchF2 = [heroPunchAtlas textureNamed: @"heroPunchF2"];
      
      NSArray *heroPunchTextures = @[punchF1, punchF2];
      
      SKAction *heroPunch = [SKAction animateWithTextures: heroPunchTextures timePerFrame: 0.1];
      
      // The trick is here, where you have the completion callback 
      // which is fired once the animation is done. If you place the 
      // [self setUserInteractionEnabled: YES]; as the last line, it will get called
      // before the animation finishes (clock cycles run faster than animation).
      [hero runAction: hulkPunch completion: ^(void){
          [self setUserInteractionEnabled: YES];
      }];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-24
        • 1970-01-01
        • 1970-01-01
        • 2022-07-08
        • 2013-12-27
        • 1970-01-01
        • 1970-01-01
        • 2017-04-15
        相关资源
        最近更新 更多