Sunday 15 June 2014

ios - pausing spritekit game on app launch / exit .. iOS8 -



ios - pausing spritekit game on app launch / exit .. iOS8 -

i've read find on topic , still cant figure out issue. have tried pausing 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) }

in controller:

override func viewdidload() { nsnotificationcenter.defaultcenter().addobserver(self, selector: "pausegame:", name: "pausegamescene", object: nil) } func pausegame(){ self.skview.paused = true self.skview.scene!.paused = true }

i know pausegame works because if toggle button in scene, stop game. if pause skview , scene straight after loaded in controller.. game not paused on launch. it's easy pause game when i'm in-game. reason whenever exit , resume app, game un-pause itself.

i notice if hacky , utilize kind of delay.. can work. stupid.. need know game 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 way maintain view paused after returning background mode.

in storyboard,

1) alter class of view myview

in view controller,

2) define skview subclass boolean named staypaused

class myview: skview { var staypaused = false bool override var paused: bool { { homecoming super.paused } set { if (!staypaused) { super.paused = newvalue } staypaused = false } } func setstaypaused() { if (super.paused) { self.staypaused = true } } }

3) define view myview

4) add together notifier set staypaused flag

class gameviewcontroller: uiviewcontroller { override func viewdidload() { super.viewdidload() if allow scene = gamescene.unarchivefromfile("gamescene") as? gamescene { // configure view. allow skview = self.view myview nsnotificationcenter.defaultcenter().addobserver(skview, selector:selector("setstaypaused"), name: "staypausednotification", object: nil)

in app delegate,

5) post notification set remain paused flag when app becomes active

func applicationdidbecomeactive(application: uiapplication) { nsnotificationcenter.defaultcenter().postnotificationname("staypausednotification", object:nil) }

ios swift ios8 sprite-kit nsnotificationcenter

No comments:

Post a Comment