错误:尝试添加已有父级的SKNode [英] Error: Attemped to add a SKNode which already has a parent
问题描述
我正在使用Swift 3和SpriteKit进行游戏,我正在尝试声明一个全局变量,以便在其余的GameScene类中使用它,但我不能。我做了什么:
I'm doing a game with Swift 3 and SpriteKit and I'm trying to declare a global variable to work with it in the rest of the GameScene class but I can't. What I did:
class GameScene: SKScene {
...
let personaje = SKSpriteNode(imageNamed: "Ball2.png")
...
全球化后声明我试图在sceneDidLoad中使用它:
After the global declaration I tried to use it in the sceneDidLoad just like that:
...
personaje.position = CGPoint.zero
addChild(personaje)
...
我不知道为什么但Xcode返回此错误:
I don't know why but Xcode returns this error:
***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'尝试添加SKNode哪个
已经有一个父:name:'(null)'
texture:['Ball2.png'(150 x 146)] position:{0,0}
scale:{1.00 ,1.00}尺寸:{150,146}锚:{0.5,0.5}旋转:0.00'
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: name:'(null)' texture:[ 'Ball2.png' (150 x 146)] position:{0, 0} scale:{1.00, 1.00} size:{150, 146} anchor:{0.5, 0.5} rotation:0.00'
提前感谢您的想法和解决方案!
Thanks in advance for your ideas and solutions!
推荐答案
我怀疑你试图添加一个已经拥有父节点的SKNode,这是不可能的。
I suspect you attempted to add a SKNode which already has a parent, which is not possible.
在将其添加到新父节点之前,从普通父节点中删除该节点:
Remove the node from the prevous parent before adding it to a new one:
personaje.removeFromParent();
addChild(personaje)
或创建一个新节点:
let newPersonaje = SKSpriteNode(imageNamed: "Ball2.png")
addChild(newPersonaje)
这篇关于错误:尝试添加已有父级的SKNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!