ios - Sprite Kit 中暂停游戏 : Why doesn't this work?

标签 ios objective-c sprite-kit

因此,我在游戏中添加了一个暂停按钮,并且 View 的卡住和解冻有效。但是,如果 View 当前被卡住,我希望它在屏幕上显示一条消息“暂停”。但如果我触摸暂停按钮,它不会显示标签。这真的很奇怪,因为我发现如果我将设备切换到横向模式,就会出现“暂停”消息。 (反之亦然,所以如果我在横向模式下触摸按钮并将设备转为纵向,则会出现暂停标签) 有人能解决这个问题吗?

顺便说一句:我还尝试了不同的方法来做到这一点,比如在场景开始时初始化暂停标签,并在需要时隐藏/显示它。这也行不通。 我还尝试在更新方法中使用 if 语句来执行此操作(如果场景暂停 => 显示暂停标签),但这也不起作用。

我之前在这里和苹果开发论坛上问过这个问题,但还没人能解决这个问题。有人认为我会在 viewwilllayoutsubviews 中呈现场景,这可以解释纵向-横向行为,但我从未在整个应用程序代码中使用该方法。

 -(void)gameScene{


 //Pause
    pauseButton = [SKSpriteNode spriteNodeWithImageNamed:@"pause.jpg"];
    pauseButton.position = CGPointMake(screenWidth/2, screenHeight-80);
    pauseButton.zPosition = 5;
    pauseButton.name = @"pauseButton";
    pauseButton.size = CGSizeMake(80, 80);

    [self addChild:pauseButton];

    //Pause Label (wenn Pause gedrückt wird)
    pauseLabel = [SKLabelNode labelNodeWithFontNamed:@"Arial"];
    pauseLabel.text = @"PAUSE";
    pauseLabel.fontSize = 70;
    pauseLabel.position = CGPointMake(screenWidth/2, screenHeight/2);
    pauseLabel.zPosition = 5;
    pauseCount = 1;
}




-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

//Pause Button
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];

    SKNode *node = [self nodeAtPoint:location];


    if ([node.name isEqualToString:@"pauseButton"]) {            

        pauseCount++;


        if (pauseCount%2 == 0) {
            self.scene.view.paused = YES;
            [self addChild:pauseLabel];

        }

        else{
            self.scene.view.paused = NO;
            [pauseLabel runAction:remove];
        }


    }

}

最佳答案

我在暂停方面遇到了类似的问题,并发现该问题与以下事实有关:尽管将暂停标签添加到节点树中,但场景在显示之前已暂停。我使用的解决方案是使用 SKAction 依次运行标签添加和暂停。

SKAction *pauseLabelAction = [SKAction runBlock:^{
   [[self scene] addChild:pauseLabel];
}];

SKAction *delayAction = [SKAction waitForDuration:0.1]; // Might not be needed.

SKAction *pauseSceneAction = [SKAction runBlock:^{
   [[[self scene] view] setPause:YES];
}];

SKAction *sequence = [SKAction sequence:@[pauseLabelAction, delayAction, pauseSceneAction]];
[self runAction:sequence];

我还没有对此进行测试,但可能还需要有一个小的延迟,以便在触发暂停之前给标签时间显示。

关于ios - Sprite Kit 中暂停游戏 : Why doesn't this work?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23952439/

相关文章:

iOS 9、Swift 2.0、Xcode 和测试

ios - UITextView、NSAttributedString 和自定义属性

ios - UITableView 部分标题的自动布局问题

ios - SpriteKit/Swift - 当两个节点已经接触时如何检查它们的接触

ios - UITextField shouldChangeCharactersInRange 触发两次?

ios - 使用 mosquitto 的 iphone 推送通知

ios - 我如何解决 NSCocoaErrorDomain :257 when pulling a file from the Files app?

javascript - 正则表达式无效 : invalid group specifier name - a regex expression that breaks on any iOS device

ios - SKScene didChangeSize : called multiple times

ios - 将 UIView 转换为 SKView 时的程序集崩溃报告