gpt4 book ai didi

ios - 重新启动并在 GameScene 中呈现

转载 作者:行者123 更新时间:2023-11-30 14:02:31 27 4
gpt4 key购买 nike

您好,我的问题是,当我单击按钮重新启动游戏时,它会重新启动,但如果我单击转换到 MainMenuViewController 的按钮,场景就会卡住。有趣的是,如果我重新启动游戏并首先单击segue按钮,它将正常工作,但如果我单击重新启动按钮,游戏就会卡住。当点击 segue 按钮并且它崩溃时,我得到错误 has no segue withidentifier 'GameToMain' 当它确实第一次工作时。当我点击重新启动游戏时,它就会崩溃,没有错误。以下是该问题的相关代码:

class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let skView = view as! SKView
let scene = GameScene(size: skView.bounds.size)
skView.showsFPS = true
skView.showsNodeCount = true
skView.ignoresSiblingOrder = false
//skView.showsPhysics = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)
scene.viewController = self
}
}

class GameScene: SKScene, SKPhysicsContactDelegate {
var viewController: GameViewController!

这是在玩家失败时调用的函数中设置的:

    let tryAgain = SKLabelNode(fontNamed: "Chalkduster")
tryAgain.text = "Try Again?"
tryAgain.color = SKColor.yellowColor()
tryAgain.name = "retryLabel"
tryAgain.fontSize = 28
tryAgain.position = CGPoint(x: size.width/2, y: size.height/2)
playerLayerNode.addChild(tryAgain)

let mainMenuTransition = SKLabelNode(fontNamed: "Chalkduster")
mainMenuTransition.text = "Main Menu"
mainMenuTransition.color = SKColor.yellowColor()
mainMenuTransition.name = "mainMenuTransitionSeque"
mainMenuTransition.fontSize = 20
mainMenuTransition.position = CGPoint(x: size.width/2, y: size.height/2 - 60)
playerLayerNode.addChild(mainMenuTransition)

这就是标签注册并对被点击使用react的方式

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in (touches ) {
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)
if (node.name == "retryLabel") {
print("restart")
let gameScene = GameScene(size: self.size)
let transition = SKTransition.doorsCloseHorizontalWithDuration(0.5)
gameScene.scaleMode = SKSceneScaleMode.AspectFill
gameScene.viewController = GameViewController()
self.scene!.view?.presentScene(gameScene, transition: transition)
print("complete Reload")
}

if (node.name == "mainMenuTransitionSeque") {
print("go to main menu")
self.viewController!.performSegueWithIdentifier("GameToMain", sender: self)
print("complete2")

}

最佳答案

我用当前代码解决了问题:

class GameViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
let skView = view as! SKView
let scene = GameScene(size: skView.bounds.size)

//skView.showsFPS = true
//skView.showsNodeCount = true
skView.ignoresSiblingOrder = false
//skView.showsPhysics = true
scene.scaleMode = .AspectFill
scene.viewController = self
skView.presentScene(scene)

}

}

在游戏场景中

var viewController: GameViewController!
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)
if (node.name == "retryLabel") {

let skView = view as SKView!
let scene = GameScene(size: skView.bounds.size)
skView.ignoresSiblingOrder = false
scene.scaleMode = .AspectFill
scene.viewController = viewController.self
skView.presentScene(scene)
}

if (node.name == "mainMenuTransitionSeque") {
self.viewController.dismissViewControllerAnimated(false, completion: nil)
print("complete2")
}

}

}
}

所以基本上为了重新启动场景,我将 GameViewController 中的代码复制并粘贴到 GameScene 中,以便它能够准确地重新启动场景,但关键是重新声明 viewController 变量。然后,为了进入主菜单,我关闭了 View Controller (GameScene),而不是创建一个新的转场,这样我就不会不断添加越来越多的场景。

关于ios - 重新启动并在 GameScene 中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32814126/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com