【问题标题】:How to cause a screen transition when an SKSpriteNode is tapped点击 SKSpriteNode 时如何导致屏幕转换
【发布时间】:2014-06-05 05:30:51
【问题描述】:

我正在使用 spritekit 框架构建一个 iOS 应用程序,并且我试图在用户点击“开始”按钮时使屏幕转换到下一个场景。我能够让屏幕转换并加载下一个场景,但是当用户点击屏幕上的任何地方时就会发生这种情况。我只希望当用户点击“开始”精灵节点时发生这种情况。我使用 SKSpriteNode 创建了开始按钮,并使用 name 属性将其命名为 startButton。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
SKAction *doNothing = [SKAction moveByX:0 y:0 duration:0];

if (startButton != nil)
{
    [startButton runAction:doNothing completion:^{
        SKScene *gamescene = [[GameScene alloc] initWithSize:self.size];
        SKTransition *transition = [SKTransition doorsCloseHorizontalWithDuration:0.0];
        [self.view presentScene:gamescene transition:transition];
    }];
}

}

谢谢!

【问题讨论】:

    标签: ios objective-c xcode sprite-kit


    【解决方案1】:

    代替

    if(startButton != nil){ }
    

    尝试在 touchesBegan 方法中使用它

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];
    
    if([node.name isEqualTo:@"startButton"]){
      //start scene
    }
    

    确保您将开始按钮的名称(标签或精灵)设置为这样

    start.name = @"startButton";
    

    编辑:

    不要使用

    [startButton runAction:doNothing completion:^{
       ....
    }
    

    相反,只需立即过渡

    if([node.name isEqualTo:@"startButton"]){
      SKScene *gamescene = [[GameScene alloc] initWithSize:self.size];
      SKTransition *transition = [SKTransition doorsCloseHorizontalWithDuration:0.0];
      [self.view presentScene:gamescene transition:transition];
    }
    

    【讨论】:

    • 嗯。我尝试按照您所说的插入代码,但是现在当我单击它时,它不会转换到下一个场景(没有任何反应)。可能是因为我使用“@property”和“@synthesize”语法为 startButton 创建了 SKSpriteNode?而且我确定使用 .name 属性完全按照您上面提到的方式命名开始按钮
    • 啊,我发现我的错误是语法错误!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 2017-04-09
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多