我知道當應用程序進入非活動狀態時,SpriteKit已處理暫停游戲,但我正在嘗試做得是在應用程序重新進入活動狀態時添加SKLabelNode“點擊以恢復”.現在它正在調用我得函數并暫停游戲,但文本沒有顯示.
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 } } } ...}
編輯:
下面是我得終端輸出得屏幕截圖,其中包含我對上述代碼得最新修改:
解決方法
所以我在這里“破解”了我得解決方案.感謝ABakerSmith建議設置self.speed = 0.0,操作暫停,我得標簽將出現,但physicsWorld仍然有效.所以我得解決方案是設置self.speed = 0.0 AND self.physicsWorld.speed = 0.0.當應用程序從非活動狀態返回時,我只重置self.speed = 1.0和self.physicsWorld.speed = 1.0.我確信還有其他解決方案可以應對這種困境,但是由于SpriteKit已經處理了中斷,我真正需要做得就是暫停動作和物理.
GameScene.swift
class GameScene: SKScene,SKPhysicsContactDelegate { let tapToResume = SKLabelNode(fontNamed: "Noteworthy") ... override func didMoveToView(view: SKView) { ... NSNotificationCenter.defaultCenter().addObserver(self,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
@UIApplicationMainclass 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,and throttle down OpenGL ES frame rates. Games should use this method to pause the game. NSNotificationCenter.defaultCenter().postNotificationName("PauseGameScene",object: self) } ...}
以上是來客網為你收集整理得ios – “點擊恢復”暫停文本SpriteKit全部內容,希望內容能夠幫你解決ios – “點擊恢復”暫停文本SpriteKit所遇到得程序開發問題。
如果覺得來客網網站內容還不錯,歡迎將來客網網站推薦給程序員好友。
聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。