简体   繁体   English

在Spritekit(Swift)中实现“退出游戏”选项的最简单方法是什么?

[英]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. 我希望我的游戏在用户触摸“游戏退出”选项时退出,但是我似乎在场景变量/引用中找不到像系统退出或close或quit属性之类的东西。 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. “退出游戏”应该等同于用户能够在其iPad上退出游戏应用程序,因此,据我推测,当我在模拟器上对其进行测试时,应该完全退出模拟器。 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 作为iOS的程序员,Apple不允许您自己退出应用程序,请在此处查看原因: 单击按钮时退出应用程序-iOS

Anyway, here is the answer to your question. 无论如何,这是您问题的答案。

First, create a SKNode displaying the quit image or text you prefer. 首先,创建一个SKNode显示您喜欢的退出图像或文本。

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: 将此SKNode添加为相机的子级,以使此图像或文本在屏幕上固定在某个位置:

myScene.camera!.addChild(myQuitImageSKNode)

Finally, subclass the Scene and override touchesBegan(_:with:) to check that the user has touched your quit image or text: 最后,对Scene进行子类化并覆盖touchesBegan(_:with:)以检查用户是否触摸了您退出的图像或文本:

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). 另一种执行此操作的方法是将与退出按钮相对应的SKNode子类化,并覆盖touchesBegan(_:with:) (这种方式无需检查名称)。

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. 最后,要知道如何退出应用程序,请查看此响应的第一个链接,或者引发异常,或调用fatalError() ...但是请注意,Apple不喜欢这样做,因此可能决定不发布您的应用程序。他们的商店。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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