【问题标题】:How to add Highlighted Button in SpriteKit?如何在 SpriteKit 中添加高亮按钮?
【发布时间】:2015-04-24 15:56:35
【问题描述】:

我知道如何在SpriteKit 中添加按钮。

我已经完成了SKSpriteNode。 但是我不知道如何添加像UIButton 这样的突出显示按钮。

我的意思是当我按下 SKSpriteNode(Button) 时,我想从 UIKit 显示像 UIButton 这样的高亮图像。

我该怎么做?

这是ButtonSKSpriteNode 的代码。

self.playButton = [SKSpriteNode spriteNodeWithImageNamed:@"playButton.png"];
self.playButton.position = CGPointMake(self.frame.origin.x + 400, 100);
self.playButton.name = @"playButton";
self.playButton.zPosition = 2;
[self addChild:self.playButton];

【问题讨论】:

    标签: ios objective-c sprite-kit skspritenode


    【解决方案1】:

    我试过了,得到了你正在寻找的东西,

      BOOL isMySpriteNodeTouched = NO;
     mySprite = [[SKSpriteNode alloc]initWithImageNamed:@"ball"];
     mySprite.position = CGPointMake(self.size.width/2.0f, self.size.height/2.0f);
     mySprite.name = @"mySprite";
    [self addChild:mySprite];
    
    
     -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    
    {
        for (UITouch *touch in touches)
          {
               CGPoint touchPoint = [touch locationInNode:self];
               SKNode *localNode = [[SKNode alloc]init];
               localNode = [self nodeAtPoint:touchPoint];
    
               if ([localNode.name isEqualToString:mySprite.name])
              {
                   mySprite.texture = [SKTexture textureWithImage:[UIImage imageNamed:@"highlightedBall"]];
                 isMySpriteNodeTouched = YES;
              }
     }
    
    
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
       {
         if(isMySpriteNodeTouched)
           {
             isMySpriteNodeTouched = !isMySpriteNodeTouched;
             mysprite.texture = [SKTexture textureWithImage:[UIImage imageNamed:@"ball"]];
            } 
       }
    

    【讨论】:

    【解决方案2】:

    一种解决方案是创建两个 SKSpriteNode,一个用于活动状态,一个用于默认状态,并将它们都添加到您的视图中。

    在您的 touchesMoved 和 touchesEnded 方法中,相应地隐藏和取消隐藏活动按钮和默认按钮。

    Here's Swift 教程。

    【讨论】:

      【解决方案3】:

      您可以使用-(void)didMoveToView:(SKView *)view方法中的view属性在SK中创建一个UIButton。

      所有 UIButton 功能,包括高亮显示都可以访问。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-23
        • 2018-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多