“Tap To Resume” Pause text SpriteKit

后端 未结 3 1406
梦谈多话
梦谈多话 2021-01-05 11:22

I know SpriteKit already handles pausing the game when the app enters the inactive state but what I\'m trying to do is add a SKLabelNode \"tap to resume\" when the app re-en

3条回答
  •  时光取名叫无心
    2021-01-05 12:06

    I believe your problem was setting self.view?.paused = true, as was pointed out by @Steve in a comment and @Linus G. in his answer. Therefore, when you tried to unhide the label, nothing happened because the view was paused.

    I tried using self.paused to pause the SKScene instead. This solved the problems of showing the label, however it didn't actually pause the scene. This could be due to: since iOS8, SpriteKit automatically pauses your game when it enters the background and un-pauses the game when it enters the foreground. Therefore, trying to set self.paused = true, using applicationWillResignActive, had no effect because it was un-paused when entering the foreground.

    To solve this you can observe UIApplicationWillResignActiveNotification. Then when the application is going to resign being active, you set self.speed = 0, which has the same effect as pausing the SKScene. This displays the label and pauses the scene as required.

    For example:

    class GameScene: SKScene {
        let tapToResume = SKLabelNode(fontNamed: "Noteworthy")
    
        override func didMoveToView(view: SKView) {
            NSNotificationCenter.defaultCenter().addObserver(self, 
                                 selector: Selector("pauseScene"), 
                                 name: UIApplicationWillResignActiveNotification, 
                                 object: nil)
    
            tapToResume.text = "tap to resume"
            tapToResume.position = CGPoint(x: frame.midX, y: frame.midY)
            tapToResume.fontSize = 55
            tapToResume.hidden = true
            self.addChild(tapToResume)
    }
    
    func pauseScene() {
        self.speed = 0.0
        tapToResume.hidden = false
    }
    
    override func touchesBegan(touches: Set, withEvent event: UIEvent) {
        // Check the label was pressed here.
        if labelWasPressed {
            self.speed = 1.0
            tapToResume.hidden = true
        }
    }
    

提交回复
热议问题