使用SKSpriteNode作为按钮从GameScene转换到UITabBarController(Use SKSpriteNode as button to transition from Game

编程入门 行业动态 更新时间:2024-10-28 05:25:36
使用SKSpriteNode作为按钮从GameScene转换到UITabBarController(Use SKSpriteNode as button to transition from GameScene to UITabBarController)

我有一个UITabBarController有两个项目,第一个是日历,它只是一个UIWebView 。 第二个项目带来一个SpriteKit游戏场景并隐藏标签栏(游戏需要全屏)。

我有一个SKSpriteNode图像,当点击时,我希望视图更改回日历视图(第一个标签栏项)。 我已经找到了一种方法来做这个,并尝试了我在StackOverflow上找到的各种建议,但没有任何工作。 该应用程序因错误而崩溃

“打开一个可选项时出乎意料地发现了nil”

我明白这个错误意味着什么,而不是为什么我得到它。 这是相关的代码(我试图弄清楚这一点时从这里得到的部分代码):

在GameScene.swift中:

weak var viewController: GameViewController! override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { let touch:UITouch = touches.first! as UITouch let positionInScene = touch.locationInNode(self) let touchedNode = self.nodeAtPoint(positionInScene) if let name = touchedNode.name { if name == "exit" { self.viewController.gameOver() self.removeFromParent() self.view?.presentScene(nil) } } }

在GameViewController.swift中:

var currentGame: GameScene! override func viewDidLoad() { super.viewDidLoad() self.tabBarController?.tabBar.hidden = true if let scene = GameScene(fileNamed:"GameScene") { // Configure the view. let skView = self.view as! SKView skView.showsFPS = false skView.showsNodeCount = false skView.ignoresSiblingOrder = true scene.scaleMode = .AspectFill scene.viewController = self skView.presentScene(scene) currentGame = scene } } func gameOver() { performSegueWithIdentifier("backToMain", sender: nil) }

这是给出错误的行:

self.viewController.gameOver()

I have a UITabBarController that has two items, the first one being a calendar which is just a UIWebView. The second item brings in a SpriteKit game scene and hides the tab bar (The game needs to be full screen).

I have an SKSpriteNode image that when tapped, I'd like the view to change back to the calendar view (the first tab bar item). I've searched for a way to do this and have tried various suggestions I've found here on StackOverflow, but nothing is working. The app crashes with the error

"Unexpectedly found nil while unwrapping an optional"

I understand what that error means, just not why I'm getting it. Here is the relevant code (part of which I got from here while trying to figure this out):

In GameScene.swift:

weak var viewController: GameViewController! override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { let touch:UITouch = touches.first! as UITouch let positionInScene = touch.locationInNode(self) let touchedNode = self.nodeAtPoint(positionInScene) if let name = touchedNode.name { if name == "exit" { self.viewController.gameOver() self.removeFromParent() self.view?.presentScene(nil) } } }

In GameViewController.swift:

var currentGame: GameScene! override func viewDidLoad() { super.viewDidLoad() self.tabBarController?.tabBar.hidden = true if let scene = GameScene(fileNamed:"GameScene") { // Configure the view. let skView = self.view as! SKView skView.showsFPS = false skView.showsNodeCount = false skView.ignoresSiblingOrder = true scene.scaleMode = .AspectFill scene.viewController = self skView.presentScene(scene) currentGame = scene } } func gameOver() { performSegueWithIdentifier("backToMain", sender: nil) }

This is the line giving the error:

self.viewController.gameOver()

最满意答案

你的代码是正确的; 我认为可能的问题是你在不传递viewController值的情况下呈现多个场景。 要关闭此问题,请使用“查找”导航器(Cmd + 3)搜索presentScene然后确保每次调用都是将viewController属性的值传递给新场景之后。

Your code is correct; I think the likely problem is that you are presenting more than one scene without transferring the value of viewController. To track this problem down, use the Find navigator (Cmd+3) to search for presentScene then ensure each call is after you have transferred the value of the viewController property across to the new scene.

更多推荐

本文发布于:2023-08-04 16:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417850.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   GameScene   SKSpriteNode   transition   button

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!