简体   繁体   中英

what's the easiest way to implement a “game quit” option in Spritekit (Swift).

I want my game to quit upon the user touching the "game quit" option, but I can't seem to find anything like a system exit or close or quit property in the scene variable/reference. The "game quit" should be the equivalent of the user being able to exit the game app on their iPad and so when I test it on the simulator, assumably, it should completely exit the simulator, as far as I deduce. Please can you assist?

May the Swift be with you :).

Thanks.

Neal

Note:

As a programmer for iOS, Apple does not allow you to exit an application yourself, see why here: exit application when click button - iOS

Anyway, here is the answer to your question.

First, create a SKNode displaying the quit image or text you prefer.

Give it a name:

myQuitImageSKNode.name = "Quit"

Add this SKNode as a child of the camera, in order for this image or text to be at a fixed position on the screen:

myScene.camera!.addChild(myQuitImageSKNode)

Finally, subclass the Scene and override touchesBegan(_:with:) to check that the user has touched your quit image or text:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch: UITouch = touches.first!
    let touchedNode = self.atPoint(touch.location(in: self))

    if touchedNode.name == "Quit" {
        // exit from your app
    }
}

Another way to do the same is to subclass the SKNode corresponding to the exit button and override touchesBegan(_:with:) (this way, no need to check the name).

Finally, to know how to exit the app, look at the first link of this response, or throw an exception, or call fatalError() ... But be careful, Apple does not like this and could decide not to publish your app on their store.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM