0

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

Neal
  • 27
  • 4

1 Answers1

0

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.

Alexandre Fenyo
  • 4,526
  • 1
  • 17
  • 24
  • cool thanks, I won't bother with it then, too risky by the sounds of it and would be very uncool if apple rejected the app, because of a quit button. That means my game is ready!! I assume apple provide an easy way to quit apps, like a small x or something, anyway. – Neal Aug 05 '18 at 17:37
  • There is a button on the device, to exit apps (they stay in background, and activities are stopped, until memory is required by another task or the user switches to the app again) – Alexandre Fenyo Aug 05 '18 at 17:40
  • 1
    Yes, marked your answer with a green tick :). A button? What if the user just wants to exit the game but keep all other apps open or close multiple apps including the game at the same time, and keep others open. Hopefully, this exit routine, if you like, is supported right?? – Neal Aug 05 '18 at 22:29
  • The user can decide to terminate an app or to let it wait in the background, in a pause state. But the user can not ask the system to keep an app in the background if the system needs memory. Moreover, the way the user terminates an app or send it in the background is sometimes changing when the system is updated to a new version, and does not depend on the app. As said in one of the answers in the link you have given at the beginning of your question, exit() is supported, but *it is not recommended by Apple and won't be accepted by the AppStore.* – Alexandre Fenyo Aug 05 '18 at 22:41
  • The reason why Apple does not want an app to choose to exit from the system, instead of being stopped waiting in the background, is because Apple is very much concerned about battery autonomy. Stopping an app (i.e. not exiting the app, but simply keeping its memory state), and restarting this app one or two days after, for instance, needs far less energy than exiting the app and restarting it later (restarted an app that exited previously means initializing again many data structures, but restarting an app that has simply been paused does not need to compute anything) – Alexandre Fenyo Aug 05 '18 at 22:58
  • cool, I see what you mean. So essentially, the unused app is put in the background and paused, then unpaused upon use, makes sense. Clever thinking from the Apple people, once more :). – Neal Aug 06 '18 at 14:23