"Tap To Resume" Pause SpriteKit Text

I know that SpriteKit already handles the suspension of the game when the application goes into an inactive state, but what I'm trying to do is add SKLabelNode β€œclick to resume” when the application goes into active state. Now he correctly calls my functions and pauses the game, but the text is not displayed.

AppDelegate.swift

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. println("applicationWillResignActive") NSNotificationCenter.defaultCenter().postNotificationName("PauseGameScene", object: self) NSNotificationCenter.defaultCenter().postNotificationName("ShowPauseText", object: self) ... } 

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) tapToResume.text = "tap to resume" tapToResume.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame)) tapToResume.fontSize = 55 tapToResume.hidden = true self.addChild(tapToResume) ... } func pauseGameScene() { println("pause game") self.view?.paused = true } func showPauseText() { if self.view?.paused == true { tapToResume.hidden = false println("show text") } } override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { ... if self.paused { self.view?.paused = false if tapToResume.hidden == false { tapToResume.hidden = true } } } ... } 

EDIT:

Below is a screenshot of my terminal output with my latest changes in my code above: enter image description here

+6
source share
3 answers

So, I β€œcracked” my decision here. Thanks to ABakerSmith with the suggestion setting self.speed = 0.0 actions were paused and my label will appear, but physicsWorld is still active. So I decided to set self.speed = 0.0 AND self.physicsWorld.speed = 0.0 . When the application returns from an inactive state, I simply 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 interrupts, I really needed to pause actions and 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<NSObject>, 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) } ... } 
+4
source

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

I tried using self.paused to pause SKScene . This solved the problem with the demonstration of the label, but in reality it did not stop the scene. This may be due to: since iOS8, SpriteKit automatically pauses your game when it enters the background, and disconnects the game when it comes to the fore. Therefore, the attempt to set self.paused = true using applicationWillResignActive had no effect, because when it entered the foreground, it was not suspended.

To solve this problem, you can observe UIApplicationWillResignActiveNotification . Then, when the application resigns, it will be set to self.speed = 0 , which has the same effect as pausing SKScene . This displays the cue and pauses the scene as needed.

For instance:

 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<NSObject>, withEvent event: UIEvent) { // Check the label was pressed here. if labelWasPressed { self.speed = 1.0 tapToResume.hidden = true } } 
+3
source

I think I have a problem. You must call pauseGameScene() . Then view?.paused is true . Then you can call showPauseText() .

Hope that helps :)

+2
source

Source: https://habr.com/ru/post/985546/


All Articles