为什么我在 Swift 中不断收到此错误? 'Attemped to add a SKNode which already has a parent'
Why do I keep getting this error in Swift? 'Attemped to add a SKNode which already has a parent'
我试图让这个矩形重复,但我一直收到这样的错误消息。我尝试了一切,但我似乎无法弄清楚。有人可以帮忙吗?太令人沮丧了,我无法通过这一部分。我已经被这个错误困住了几天了。如果您需要更多信息,请给我发消息。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: Attemped to add a SKNode which already has a parent: >name:'(null)' texture:[ 'longrec'
class GameScene: SKScene, SKPhysicsContactDelegate {
let longRec = SKSpriteNode(imageNamed: "longrec")
override func didMoveToView(view: SKView) {
repeatlongRec()
}
func longRec() {
let moveToTheLeft = SKAction.moveByX(-self.size.width, y: 0, duration: 2.0)
let repeatMoveToLeft = SKAction.repeatActionForever(moveToTheLeft)
let removeFromScene = SKAction.removeFromParent()
let sequence = SKAction.sequence([repeatMoveToLeft, removeFromScene])
longRec.position = CGPointMake(self.size.width / 0.5, self.size.height / 2)
longRec.zPosition = 15
longRec.runAction(sequence)
addChild(longRec)
}
func repeatlongRec() {
let generateLongRec = SKAction.sequence([
SKAction.runBlock(self.longRec),
SKAction.waitForDuration(2.0)])
let endlessAction = SKAction.repeatAction(generateLongRec, count: 4)
runAction(endlessAction)
}
}
在你执行 addChild(longRec) 之后你永远不会删除它
因此,当您执行 func repeatlongRec() 时,它会崩溃,因为该节点仍然存在。
尝试重组这两个函数,以便在不使用节点时删除节点,然后重复该过程
试试下面的代码:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let longRec = SKSpriteNode(imageNamed: "rectangle.png")
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let makeLongRec = SKAction.sequence([SKAction.runBlock(generateLongRec), SKAction.waitForDuration(3)])
runAction(SKAction.repeatAction(makeLongRec, count: 4))
}
func generateLongRec() {
longRec.position = CGPointMake(self.size.width / 2, self.size.height / 2)
longRec.zPosition = 15
self.addChild(longRec)
let moveToTheLeft = SKAction.moveByX(-self.size.width, y: 0, duration: 2.0)
let removeFromScene = SKAction.removeFromParent()
longRec.runAction(SKAction.sequence([moveToTheLeft, removeFromScene]))
}
您的代码存在问题,添加速度太快。就像@Ace Green 所说的那样,您甚至在删除节点之前就添加了该节点。
另外两件事你应该知道:
- 尽量不要为
func
和您的节点使用相同的名称。
- 如果您打算只使用一个节点并重复添加它。
didMoveToView
中 waitforDuration(2)
中的等待时间应始终大于 let moveToTheLeft = SKAction.moveByX(-self.size.width, y: 0, duration: 2.0)
中的持续时间,否则你仍然会遇到相同的 Attemped to add a SKNode which already has a parent
问题。解决这个问题的一种方法是声明一个节点数组。
我会尝试这样做:
class GameScene: SKScene, SKPhysicsContactDelegate {
let longRec = SKSpriteNode(imageNamed: "longrec")
override func didMoveToView(view: SKView) {
var timer = NSTimer .scheduledTimerWithTimeInterval(2, target: self, selector: Selector("longRec") , userInfo: nil, repeats: true)
}
func longRec() {
let moveToTheLeft = SKAction.moveByX(-self.size.width, y: 0, duration: 2.0)
let removeFromScene = SKAction.removeFromParent()
let sequence = SKAction.sequence([moveToTheLeft, removeFromScene])
let repeatSequence = SKAction.repeatActionForever(sequence)
longRec.position = CGPointMake(self.size.width / 0.5, self.size.height / 2)
longRec.zPosition = 15
longRec.runAction(repeatSequence)
addChild(longRec)
}
}
我试图让这个矩形重复,但我一直收到这样的错误消息。我尝试了一切,但我似乎无法弄清楚。有人可以帮忙吗?太令人沮丧了,我无法通过这一部分。我已经被这个错误困住了几天了。如果您需要更多信息,请给我发消息。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: Attemped to add a SKNode which already has a parent: >name:'(null)' texture:[ 'longrec'
class GameScene: SKScene, SKPhysicsContactDelegate {
let longRec = SKSpriteNode(imageNamed: "longrec")
override func didMoveToView(view: SKView) {
repeatlongRec()
}
func longRec() {
let moveToTheLeft = SKAction.moveByX(-self.size.width, y: 0, duration: 2.0)
let repeatMoveToLeft = SKAction.repeatActionForever(moveToTheLeft)
let removeFromScene = SKAction.removeFromParent()
let sequence = SKAction.sequence([repeatMoveToLeft, removeFromScene])
longRec.position = CGPointMake(self.size.width / 0.5, self.size.height / 2)
longRec.zPosition = 15
longRec.runAction(sequence)
addChild(longRec)
}
func repeatlongRec() {
let generateLongRec = SKAction.sequence([
SKAction.runBlock(self.longRec),
SKAction.waitForDuration(2.0)])
let endlessAction = SKAction.repeatAction(generateLongRec, count: 4)
runAction(endlessAction)
}
}
在你执行 addChild(longRec) 之后你永远不会删除它
因此,当您执行 func repeatlongRec() 时,它会崩溃,因为该节点仍然存在。
尝试重组这两个函数,以便在不使用节点时删除节点,然后重复该过程
试试下面的代码:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let longRec = SKSpriteNode(imageNamed: "rectangle.png")
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let makeLongRec = SKAction.sequence([SKAction.runBlock(generateLongRec), SKAction.waitForDuration(3)])
runAction(SKAction.repeatAction(makeLongRec, count: 4))
}
func generateLongRec() {
longRec.position = CGPointMake(self.size.width / 2, self.size.height / 2)
longRec.zPosition = 15
self.addChild(longRec)
let moveToTheLeft = SKAction.moveByX(-self.size.width, y: 0, duration: 2.0)
let removeFromScene = SKAction.removeFromParent()
longRec.runAction(SKAction.sequence([moveToTheLeft, removeFromScene]))
}
您的代码存在问题,添加速度太快。就像@Ace Green 所说的那样,您甚至在删除节点之前就添加了该节点。
另外两件事你应该知道:
- 尽量不要为
func
和您的节点使用相同的名称。 - 如果您打算只使用一个节点并重复添加它。
didMoveToView
中waitforDuration(2)
中的等待时间应始终大于let moveToTheLeft = SKAction.moveByX(-self.size.width, y: 0, duration: 2.0)
中的持续时间,否则你仍然会遇到相同的Attemped to add a SKNode which already has a parent
问题。解决这个问题的一种方法是声明一个节点数组。
我会尝试这样做:
class GameScene: SKScene, SKPhysicsContactDelegate {
let longRec = SKSpriteNode(imageNamed: "longrec")
override func didMoveToView(view: SKView) {
var timer = NSTimer .scheduledTimerWithTimeInterval(2, target: self, selector: Selector("longRec") , userInfo: nil, repeats: true)
}
func longRec() {
let moveToTheLeft = SKAction.moveByX(-self.size.width, y: 0, duration: 2.0)
let removeFromScene = SKAction.removeFromParent()
let sequence = SKAction.sequence([moveToTheLeft, removeFromScene])
let repeatSequence = SKAction.repeatActionForever(sequence)
longRec.position = CGPointMake(self.size.width / 0.5, self.size.height / 2)
longRec.zPosition = 15
longRec.runAction(repeatSequence)
addChild(longRec)
}
}