如何从 SKScene 回到 UIViewController (MenuVC)?
Posted
技术标签:
【中文标题】如何从 SKScene 回到 UIViewController (MenuVC)?【英文标题】:How to get back from SKScene to UIViewController (MenuVC)? 【发布时间】:2019-03-11 18:00:05 【问题描述】:我有一个类似的问题,就像这个线程中的那个人How to get from SKScene to UIViewController?。首先我应该说我是一个完全的初学者,这对我来说都是新的。
我的主菜单(我的应用加载时的第一个屏幕)在这里:
class MenuVC: UIViewController
...
我的应用(游戏)的交互可以在这里进行:
//
// GameScene.swift
// Doodle Pong
//
// Created by Daniel Kloe on 28.02.17.
// Copyright © 2017 Daniel Kloe. All rights reserved.
//
import SpriteKit
import GameplayKit
import Foundation
import UIKit
class GameScene: SKScene
//let storyboard = UIStoryboard(name: "Main", bundle: nil)
//let MenuVC = storyboard.instantiateViewController(withIdentifier: "MenuVC")
//MenuVC.view.frame = (self.view?.frame)!
//MenuVC.view.layoutIfNeeded()
//UIView.transition(with: self.view!, duration: 0.3, options: .transitionFlipFromRight, animations:
//
//self.view?.window?.rootViewController = vc
//, completion: completed in
//)
var ball = SKSpriteNode()
var enemy = SKSpriteNode()
var main = SKSpriteNode()
var score = [Int] ()
var topLbl = SKLabelNode()
var btmLbl = SKLabelNode()
override func didMove(to view: SKView)
topLbl = self.childNode(withName: "topLabel") as! SKLabelNode
btmLbl = self.childNode(withName: "btmLabel") as! SKLabelNode
ball = self.childNode(withName: "ball") as! SKSpriteNode
enemy = self.childNode(withName: "enemy") as! SKSpriteNode
main = self.childNode(withName: "main") as! SKSpriteNode
let border = SKPhysicsBody(edgeLoopFrom: self.frame)
border.friction = 0
border.restitution = 1
self.physicsBody = border
startGame()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
for touch in touches
// let gameSceneTemp = GameScene(fileNamed: "GameScene")
// self.scene?.view?.presentScene(gameSceneTemp, transition: SKTransition.doorsCloseHorizontal(withDuration: 0.01))
let location = touch.location(in: self) //die location wird mit dem Berühren auf dem Bildschirm beschrieben
if currentGameType == .TwoPlayer
if location.y > 0
enemy.run(SKAction.moveTo(x: location.x, duration: 0.01)) //Der "main Balken" wird zu den location bewegt
if location.y < 0
main.run(SKAction.moveTo(x: location.x, duration: 0.01)) //Der "main Balken" wird zu den location bewegt
else
main.run(SKAction.moveTo(x: location.x, duration: 0.01)) //Der "main Balken" wird zu den location bewegt
func startGame()
score = [0,0]
topLbl.text = "\(score[1])"
btmLbl.text = "\(score[0])"
//let delay = SKAction.wait(forDuration: 2000)
//self.run(delay) //evtl. Wartezeit 2s, funktioniert noch nicht richtig
ball.physicsBody?.applyImpulse(CGVector(dx: 20, dy: 20))
func addScore(playerWhoWon : SKSpriteNode)
ball.position = CGPoint(x: 0, y: 0)
ball.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
if playerWhoWon == enemy
score[1] += 1
ball.physicsBody?.applyImpulse(CGVector(dx: 32, dy: 27))
else if playerWhoWon == main
score[0] += 1
ball.physicsBody?.applyImpulse(CGVector(dx: -32, dy: -27))
topLbl.text = "\(score[1])"
btmLbl.text = "\(score[0])"
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)
for touch in touches
let location = touch.location(in: self) //die location wird mit dem Berühren auf dem Bildschirm beschrieben
if currentGameType == .TwoPlayer
if location.y > 0
enemy.run(SKAction.moveTo(x: location.x, duration: 0.01)) //Der "main Balken" wird zu den location bewegt
if location.y < 0
main.run(SKAction.moveTo(x: location.x, duration: 0.01)) //Der "main Balken" wird zu den location bewegt
else
self.topLbl.zRotation = CGFloat(2 * M_PI)
// UIView.animate(withDuration: 0, animations: (
// self.topLbl.zRotation = CGFloat(2 * M_PI) //CGFloat(.pi / 4.0)
// ))
main.run(SKAction.moveTo(x: location.x, duration: 0.01)) //Der "main Balken" wird zu den location bewegt
override func update(_ currentTime: TimeInterval)
switch currentGameType
case .Easy:
enemy.run(SKAction.moveTo(x: ball.position.x, duration: 1.0))
break
case .Medium:
enemy.run(SKAction.moveTo(x: ball.position.x, duration: 0.5))
break
case .Extreme:
enemy.run(SKAction.moveTo(x: ball.position.x, duration: 0.08))
break
case .TwoPlayer:
break
//Called before each frame is rendered
if ball.position.y <= main.position.y - 20
addScore(playerWhoWon: enemy)
else if ball.position.y >= enemy.position.y + 20
addScore(playerWhoWon: main)
@IBAction func backToMainMenu(_ sender: Any)
//self.view?.window?.rootViewController
self.view?.window!.rootViewController?.dismiss(animated: false, completion: nil)
在我的函数“backToMainMenu”中,我尝试返回 MenuVC,但收到警告“'UIViewController 类型的表达式?'未使用”,如果我在模拟器中尝试,模拟器会崩溃。
感谢您提供的各种帮助 :)。
【问题讨论】:
【参考方案1】:尝试:
self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)
这应该关闭 rootViewController 上方的所有视图
编辑:
当我们定义 ViewController 时,这可能对您有用,请确保在您的 Storyboard 中您的 MenuVC
有一个标识符:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MenuVC") // Make sure is the same identifier as in the storyboard.
vc.view.frame = (self.view?.frame)!
vc.view.layoutIfNeeded()
UIView.transition(with: self.view!, duration: 0.3, options: .transitionFlipFromRight, animations:
self.view?.window?.rootViewController = vc
, completion: completed in
)
编辑:
似乎最好的方法是将您的 SKScene 类托管在另一个视图控制器中。我建议在您的 SKScene 所在的位置创建另一个 ViewController,例如 SKViewController
。将MenuVC
嵌入到navigationController
中,在storyBoard 中单击它,单击编辑器(从顶部菜单中)> 嵌入> 导航控制器。编辑你的 MenuVC,这样你就不用打开你的场景,而是转到 SKViewController,然后在那里打开场景。为此,只需在情节提要中从MenuVC
创建一个segue,例如使用标识符“MenuToSKScene”,当您想要打开场景时,只需从 MenuVC 中执行以下操作:
self.performsegue(with identifier: "MenuToSKScene", sender: self)
然后在SKViewController
中执行您当前在MenuVC
中执行的任何操作,而不是打开场景。
这会将您发送到具有您的 SKScene 类的 SKViewController。那么当你想回到菜单时,你所要做的就是:
self.dismiss(animated: true, completion: nil)
或使用标识符“SKSceneToMenu”创建从 SKViewController 到 MenuVC 的 segue 并调用:
self.performsegue(with identifier: "SKSceneToMenu", sender: self)
所以本质上,不要从MenuVC
打开SKScene
,而是有一个单独的viewController SKViewController
,当你想打开你的场景时,你可以打开它,这样你就可以轻松地跳回菜单。
【讨论】:
我不得不将代码稍微更改为 --> self.view?.window!.rootViewController?.dismiss(animated: false, completion: nil)。语法现在似乎是正确的,但是如果我按下主菜单按钮,应用程序就会崩溃(-> 0x108929e28 : movq -0x20(%rbp), %rdi)。 您的 MenuVC 是否在 navigationController 中? @danichloé 在我的编辑中尝试上面的解决方案,它应该从定义 ViewController 的 SKScene 工作,然后使用 Transition。 谢谢。我的 MenuVC 位于与导航控制器有关系的视图控制器中。你的意思是“......确保在你的 Storyboard 中你的 MenuVC 有一个标识符”,我必须在我的 MenuVC 的视图控制器中插入一个 Storyboard ID? @edey 是的,没错,(withIdentifier: "MenuVC")
与您为 MenuVC 提供的 Storyboard ID 相同(我只是将 MenuVC 作为标识符)以上是关于如何从 SKScene 回到 UIViewController (MenuVC)?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 SKScene 呈现 UIAlertController
如何从 SKScene 到 UIViewController?