在应用程序启动/退出时暂停spritekit游戏.. iOS8 [英] pausing spritekit game on app launch / exit .. iOS8
问题描述
我已经阅读了有关此主题的所有内容,但仍然无法弄清楚我的问题。我已经尝试在appdelegate的每个区域暂停我的游戏
I've read everything I could find on this topic and still cant figure out my issue. I have tried pausing my game in every area of appdelegate
func applicationWillResignActive(application: UIApplication!) {
NSNotificationCenter.defaultCenter().postNotificationName("pauseGameScene", object: self)
}
func applicationDidEnterBackground(application: UIApplication!) {
NSNotificationCenter.defaultCenter().postNotificationName("pauseGameScene", object: self)
}
func applicationWillEnterForeground(application: UIApplication!) {
NSNotificationCenter.defaultCenter().postNotificationName("pauseGameScene", object: self)
}
func applicationDidBecomeActive(application: UIApplication!) {
NSNotificationCenter.defaultCenter().postNotificationName("pauseGameScene", object: self)
}
在我的控制器中:
override func viewDidLoad() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "pauseGame:", name: "pauseGameScene", object: nil)
}
func pauseGame(){
self.skView.paused = true
self.skView.scene!.paused = true
}
我知道pauseGame有效,因为如果我用一个按钮切换它我的场景,它将停止游戏。即使我将skview和场景加载到控制器后直接暂停我的场景和场景,游戏也不会在启动时暂停。当我在游戏中时,很容易暂停游戏。但出于某种原因,每当我退出并恢复应用程序时,游戏都会自行暂停。
i know pauseGame works because if i toggle it with a button in my scene, it will stop the game. Even if I pause my skview and scene directly after they are loaded in the controller.. the game will not be paused on launch. It's easy to pause the game when I'm in-game. but for some reason whenever i exit and resume the app, the game will un-pause itself.
我注意到如果我得到hacky并使用某种延迟..我可以让它工作。但显然这是非常愚蠢的......我只需要知道游戏本身在哪里取消!
i notice if i get hacky and use some kind of delay.. i can get it to work. but obviously this is very stupid.. i just need to know where the game is unpausing itself!
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
func pauseGame(sender: UIButton!){
delay(2) {
println("blah")
self.skView.paused = true
self.skView.scene!.paused = true
}
}
推荐答案
这是从后台模式返回后保持视图暂停的一种方法。
Here's a way to keep the view paused after returning from background mode.
Xcode 7 (请参阅下面的Xcode 8说明)
Xcode 7 (see below for Xcode 8 instructions)
在故事板中,
1)将视图的类更改为MyView
1) Change the class of the view to MyView
在视图控制器中,
2)定义一个SKView子类一个名为stayPaused的布尔值
2) Define an SKView subclass with a boolean named stayPaused
class MyView: SKView {
var stayPaused = false
override var paused: Bool {
get {
return super.paused
}
set {
if (!stayPaused) {
super.paused = newValue
}
stayPaused = false
}
}
func setStayPaused() {
if (super.paused) {
self.stayPaused = true
}
}
}
3)将视图定义为MyView
3) Define the view as MyView
4)添加通知程序以设置stayPaused标志
4) Add a notifier to set the stayPaused flag
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as MyView
NSNotificationCenter.defaultCenter().addObserver(skView, selector:Selector("setStayPaused"), name: "stayPausedNotification", object: nil)
在App Delegate中,
In the App Delegate,
5)当应用变为活动状态时发布通知以设置停留暂停标志
5) Post a notification to set the stay paused flag when the app becomes active
func applicationDidBecomeActive(application: UIApplication) {
NSNotificationCenter.defaultCenter().postNotificationName("stayPausedNotification", object:nil)
}
Xcode 8
在故事板中,
1)更改类的查看从 SKView
到 MyView
1) Change the class of the view from SKView
to MyView
在视图控制器中,
2)使用名为stayPaused的布尔值定义 SKView
子类
2) Define an SKView
subclass with a boolean named stayPaused
class MyView: SKView {
var stayPaused = false
override var isPaused: Bool {
get {
return super.isPaused
}
set {
if (!stayPaused) {
super.isPaused = newValue
}
stayPaused = false
}
}
func setStayPaused() {
if (super.isPaused) {
self.stayPaused = true
}
}
}
3)将视图定义为MyView
3) Define the view as MyView
4)添加通知程序以设置stayPaused标志
4) Add a notifier to set the stayPaused flag
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! MyView? {
NotificationCenter.default.addObserver(view, selector:#selector(MyView.setStayPaused), name: NSNotification.Name(rawValue: "stayPausedNotification"), object: nil)
在App Delegate中,
In the App Delegate,
5)发布通知以设置停留暂停标志当应用程序变为活动状态时
5) Post a notification to set the stay paused flag when the app becomes active
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "stayPausedNotification"), object: nil)
}
这篇关于在应用程序启动/退出时暂停spritekit游戏.. iOS8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!