“Tap To Resume” Pause text SpriteKit

后端 未结 3 1419
梦谈多话
梦谈多话 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:00

    So I "hacked" my solution here. Thanks to ABakerSmith with the suggestion of setting self.speed = 0.0, the actions were paused and the my label will appear but the physicsWorld was still active. So my solution was to set self.speed = 0.0 AND self.physicsWorld.speed = 0.0. When the app returns from the inactive state, I just reset self.speed = 1.0 and self.physicsWorld.speed = 1.0. I'm sure there are other solutions to this dilemma but since SpriteKit already handles interruptions, all I really needed to do was pause the actions and the physics.

    GameScene.swift

    class GameScene: SKScene, SKPhysicsContactDelegate {
        let tapToResume = SKLabelNode(fontNamed: "Noteworthy")
        ...
    
        override func didMoveToView(view: SKView) {
            ...
            NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseGameScene"), name: "PauseGameScene", object: nil)
            NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("showPauseText"), name: "ShowPauseText", object: nil)
        }
    
        func pauseGameScene() {
            self.physicsWorld.speed = 0.0
            self.speed = 0.0
        }
    
        func showPauseText() {
            if self.physicsWorld.speed == 0.0 {
            tapToResume.hidden = false
            }
        }
    
        override func touchesBegan(touches: Set, withEvent event: UIEvent) {
            ...
            if self.physicsWorld.speed == 0.0 {
                self.physicsWorld.speed = 1.0
                self.speed = 1.0
                if tapToResume.hidden == false {
                    tapToResume.hidden = true
                }
            }
        }
    
        ...
    }
    

    AppDelegate.swift

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
        func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
        NSNotificationCenter.defaultCenter().postNotificationName("PauseGameScene", object: self)
        NSNotificationCenter.defaultCenter().postNotificationName("ShowPauseText", object: self)
        }
        ...
    }
    

提交回复
热议问题