【问题标题】:How to make buttons in sprite kit scenes animate as buttons (from view controller)?如何使精灵套件场景中的按钮动画为按钮(来自视图控制器)?
【发布时间】:2014-08-15 21:01:37
【问题描述】:

我正在使用 sprite kit 开发游戏,并且一直使用 SKSpriteNode 作为按钮。我做了很多研究,似乎开发游戏的“最佳实践”方式是在单个视图控制器上使用多个 sprite-kit-scenes 并在 SKScenes 之间进行转换。

但是,SKSpriteNode 没有添加到视图控制器的按钮所具有的按下动画。也就是说,如果我向视图控制器添加一个按钮并将其图像更改为 Spaceship.png,当我在 Build&Run 中单击它时,它会生成动画(看起来 alpha 属性正在更改,可能会更改为 0.5 左右)。此外,如果您按住按钮,它不会执行其操作,直到您松开。此外,如果您按住它并将鼠标/手指移开,它将返回其原始状态(不执行操作,并且 alpha 设置回 1.0)。

我想知道是否有一种方法可以为 SKSpriteNode 按钮执行这些操作?我找到了更改纹理/图像的代码(不是我想要完成的),并且我还没有找到允许 SKSpriteNode 在您松开或取消触摸效果之前不执行的代码(上一段的最后两句)。

【问题讨论】:

    标签: objective-c animation button sprite-kit


    【解决方案1】:

    感谢上面的 Sangony!使用你的逻辑我添加了一点(你有 alpha 淡入淡出,我添加了代码以使其取消事件),我只想在这里发布我的最终代码,以防其他人遇到这个:

    @implementation LTMyScene
    {
        SKSpriteNode *spriteA;
        SKLabelNode *labelCounter;
        int intCounter;
    }
    
    -(id)initWithSize:(CGSize)size {    
        if (self = [super initWithSize:size]) {
            /* Setup your scene here */
    
            intCounter=0;
            self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
    
            spriteA = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];//[SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(50, 50)];
            spriteA.size=CGSizeMake(100, 100);
            spriteA.position = CGPointMake(self.size.width/2,self.size.height/2);
            [self addChild:spriteA];
    
            labelCounter = [SKLabelNode labelNodeWithFontNamed:@"Thirteen Pixel Fonts"];
            labelCounter.fontSize=12.0;
            labelCounter.text=[NSString stringWithFormat:@"Counter is: %i",intCounter];
            labelCounter.name = @"HighScoreLabel";
            labelCounter.verticalAlignmentMode=SKLabelVerticalAlignmentModeCenter;
            labelCounter.position=CGPointMake(self.size.width/2,self.size.height/4);
            [self addChild:labelCounter];
        }
        return self;
    }
    
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        CGPoint touchLocation = [[touches anyObject] locationInNode:self];
        if([spriteA containsPoint:touchLocation]) {
            spriteA.alpha = 0.5;
        }
    }
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        CGPoint touchLocation = [[touches anyObject] locationInNode:self];
        if(spriteA.alpha == 0.5) {
            if([spriteA containsPoint:touchLocation]) {
                intCounter=intCounter+1;
                labelCounter.text=[NSString stringWithFormat:@"Counter is: %i",intCounter];
            }
            spriteA.alpha = 1.0;
        }
    }
    -(void)update:(CFTimeInterval)currentTime {
        /* Called before each frame is rendered */
    }
    
    @end
    

    【讨论】:

    • 两个建议:1) 将此代码移动到 SKSpriteNode 的子类中,称为 Button 和 2) 如果用户将手指从按钮上移开,则覆盖 touchesMoved 以设置 alpha = 1.0(以更好地模仿UIButton 的行为)。您可以在 touchesEnded 中删除带有 containsPoint 的 if 语句,因为 alpha 将是 1.0。
    【解决方案2】:
    @implementation MyScene
    {
        SKSpriteNode *spriteA;
    }
    
    -(id)initWithSize:(CGSize)size
    {
        if (self = [super initWithSize:size])
        {           
            spriteA = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(50, 50)];
            spriteA.position = CGPointMake(100, 100);
            [self addChild:spriteA];
        }
        return self;
    }
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        CGPoint touchLocation = [[touches anyObject] locationInNode:self];
        if([spriteA containsPoint:touchLocation])
            spriteA.alpha = 0.5;
    }
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        if(spriteA.alpha == 0.5)
            spriteA.alpha = 1.0;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-11-10
      • 1970-01-01
      • 2019-03-14
      • 2013-10-27
      • 2014-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多