【问题标题】:How to make an animated button in Sprite Kit如何在 Sprite Kit 中制作动画按钮
【发布时间】:2014-09-11 03:16:10
【问题描述】:

我一直在尝试实现一种方法,用户可以按下播放按钮,然后将纹理更改为按下的图像。然后,如果用户决定不继续他们的操作。比如开始游戏。然后他们可以简单地拖出精灵的框架/主体,然后不再将触摸检测为将启动动作的触摸。

这个实现的问题是,如果用户在播放按钮的 sprite 框架之外拖动,我无法取消按钮的触摸。

您将在下面看到的代码没有场景之间的过渡,因为我想在必须始终退出应用程序以尝试按钮之前测试按钮的可用性。

另外,我已经在 .h 文件中声明了大部分对象。

MenuScene.m 中的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //Touch detection declaration
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self];
    touchNode = [self nodeAtPoint:touchLocation];

    if([touchNode.name isEqualToString:@"playButton"]){
        [playButtonSprite runAction:changePlayButtonTextureON];
    }else{}

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    if ([touchNode.name isEqualToString:@"playButton"]) {
        [playButtonSprite runAction:changePlayButtonTextureOFF];
    }
}

我还想知道是否有另一种方法来检测触摸是否在播放按钮精灵节点上。不过,当找到我提到的上一个问题的解决方案时,这可能会得到解决。

【问题讨论】:

    标签: ios objective-c xcode ios7 sprite-kit


    【解决方案1】:

    处理这个问题的最好方法是继承 SKSpriteNode 来自己处理触摸。您可以在 GitHub 上的 project 中看到这样的实现。当触摸超出节点边界时取消触摸。

    您发布的现有代码很好,因为当触摸结束超出播放按钮的范围时,代码不会被调用。

    在您的情况下,由于您正在处理来自场景本身的触摸,因此只要在节点边界之外检测到触摸,您就可以显式设置播放按钮的纹理。

    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        CGPoint touchLocation = [touch locationInNode:self];
        touchNode = [self nodeAtPoint:touchLocation];
    
        if(![touchNode.name isEqualToString:@"playButton"]){
            [playButtonSprite runAction:changePlayButtonTextureOFF];
        }
    }
    

    【讨论】:

      【解决方案2】:
      - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
          //Touch detection declaration
          UITouch *touch = [touches anyObject];
          CGPoint touchLocation = [touch locationInNode:self];
          touchNode = [self nodeAtPoint:touchLocation];
      
          if([touchNode.name isEqualToString:@"playButton"]){
              [playButtonSprite runAction:changePlayButtonTextureON];
              playButtonSprite.isON = YES;
          }else{}
      }
      
      - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
          if (playButtonSprite.isON) {
              if ([touchNode.name isEqualToString:@"playButton"]) {  // The user really wants to play!
                  [self startPlaying];
              }
              [playButtonSprite runAction:changePlayButtonTextureOFF];
              playButtonSprite.isON = NO;
          }
      }
      

      其中changePlayButtonTextureON 还将playButtonSprite.isON 设置为YES,反之亦然。

      您需要为playButtonSprite 创建一个SKSpriteNode 的子类,并在此处添加该布尔属性isON

      【讨论】:

      • 当我将它添加到我的代码中时,它会给出一个错误。我认为这是因为 playButtonSprite 没有 .isON。
      • 另外,因为我试图找出如何做到这一点,以便如果用户将手指拖到 playButtonSprite 的框架之外,它将取消进入下一个场景的动作。我认为执行此操作的代码应该在 touchesBegan 方法中。
      • 你说拖动的那一刻,说明代码应该在touchesMoved方法中。
      • @johnrspicer 您将希望通过添加@property (nonatomic, assign) BOOL isON 来实现它,并进一步修改代码以让此属性指示按钮是否突出显示。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      • 2013-10-06
      • 1970-01-01
      相关资源
      最近更新 更多