我正在做一個Swift 3和SpriteKit的遊戲,我試圖在GameScene類的其餘部分聲明一個全局變量來處理它,但我可以「T。我做了什麼:錯誤:試圖添加一個已經有父母的SKNode
class GameScene: SKScene {
...
let personaje = SKSpriteNode(imageNamed: "Ball2.png")
...
全局聲明我試圖在sceneDidLoad就這樣使用它後:
...
personaje.position = CGPoint.zero
addChild(personaje)
...
我不知道爲什麼,但Xcode中返回此錯誤:
*** 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'
提前感謝您的想法和解決方案!
在'addChild(personaje)'上放置一個斷點,看看它被調用了多少次。我懷疑它被稱爲不止一次。 – Alexander