我正在创建我的第一个游戏,但它在从 GameOverScene 返回后崩溃了。这是给我错误的部分:
// GameScene.swift
// Test_Crash_1
import SpriteKit
struct global {
static var wheelRotPlat2 = SKShapeNode(circleOfRadius: 400)
static var button : SKShapeNode = SKShapeNode()
static var actualPlayer : String = String()
static let btnNextPlayer = SKShapeNode(circleOfRadius: 70)
}
class GameScene: SKScene {
deinit {
print("The GameScene has been removed from memory")
}
override func didMoveToView(view: SKView) {
let buttonDice = SKShapeNode(rectOfSize: (CGSizeMake(40,40)), cornerRadius: 5)
global.button = buttonDice
global.button.position = CGPoint(x: (CGRectGetMidX(self.frame)-400), y:(CGRectGetMidY(self.frame)-350))
global.button.fillColor = UIColor(red: 0.2, green: 1.0, blue: 0.2, alpha: 0.8)
global.button.name = "button"
global.button.zPosition = 1
addChild(global.button)
global.wheelRotPlat2.addChild(global.btnNextPlayer)
global.btnNextPlayer.name = "btnNextPlayer"
} // end didMoveToView
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches //as! Set<UITouch>
let location = touch.first!.locationInNode(self)
let node = self.nodeAtPoint(location)
if (node.name == "button") {
self.removeAllActions()
self.removeAllChildren()
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let gameOverScene = GameOverScene(size: self.size, player: global.actualPlayer)
self.view?.presentScene(gameOverScene, transition: reveal)
}
} // End func touchesBegan
} // end GameScene
和 GameOverScene 文件:
// GameOverScene.swift
// Test_Crash_1
import Foundation
import SpriteKit
class GameOverScene: SKScene {
init(size: CGSize, player: String) {
super.init(size: size)
backgroundColor = SKColor.whiteColor()
runAction(SKAction.sequence([
SKAction.waitForDuration(3.0),
SKAction.runBlock() {
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let scene = GameScene(size: size)
self.view?.presentScene(scene, transition:reveal)
}
]))
} // end init(size
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
} // end GameOverScene
这个错误是众所周知的:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: SKShapeNode name:'btnNextPlayer'
此示例只有一个绿色按钮,可将我带到 GameOverScene。当它返回时游戏崩溃。
我不明白为什么会发生这种情况。 谁能告诉我为什么会发生这种情况以及如何解决?
最佳答案
错误告诉您发生了什么,您正在将一个节点添加到另一个节点,但该节点已经有一个父节点。这就是为什么使用全局变量是一个坏主意并且几乎不应该使用的原因。您有一个全局按钮。它附加到 GameScene,让我们调用 GameScene1。然后你调用 Gameover。现在你调用一个新的 GameScene,叫做 GameScene2。由于全局变量之类的原因,您没有正确管理内存,因此 GameScene1 仍然存在。嗯按钮仍然附加到 GameScene1,所以当你尝试将它添加到 GameScene2 时,XCode 说“不,这个节点有父节点,去修复你的东西”。尽量不要使用全局数据,这样的问题应该会少一些。
编辑:
正如@Whirlwind 指出的那样,问题出在导致问题的全局项 global.wheelRotPlat2
中。
global.wheelRotPlat2.addChild(global.btnNextPlayer)
每次将 GameScene
移动到 View 时都会被调用,因为您永远不会删除 btnNextPlayer
当你杀死你的第一个场景时从它的父级,当你创建你的第二个场景时,global.btnNextPlayer
将尝试再次重新连接到同一个父级(global.wheelRotPlat2),这是导致你的问题。
关于ios - 从 GameOverScene 返回后我的游戏崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34890255/