我正在使用spritekit框架构建一个iOS应用程序,当我尝试用户点击" start"时,我试图让屏幕转换到下一个场景。按钮。我能够让屏幕转换,并加载下一个场景,但是当用户点击屏幕上的任何地方时就会发生这种情况。我只希望当用户点击"开始" spritenode。我使用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];
}];
}
}
谢谢!
答案 0 :(得分:0)
而不是
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];
}