问题
The error that I keep getting is this one: Attemped to add a SKNode which already has a parent
. I have tried to take out the spawn function and it works without it. Can someone please help me ?
func spawnTrolls(){
let minValue = self.size.width / 10
let maxValue = self.size.width - 30
let spawnpoint = UInt32(maxValue - minValue)
Troll.position = CGPointMake(CGFloat(arc4random_uniform(spawnpoint)), self.size.height)
self.addChild(Troll)
let action = SKAction.moveToY(-70, duration: 2)
let actionDone = SKAction.removeFromParent()
Troll.runAction(SKAction.sequence([action, actionDone]))
Troll.physicsBody? = SKPhysicsBody(rectangleOfSize: Troll.size)
Troll.physicsBody?.categoryBitMask = physicsCategory.Troll
Troll.physicsBody?.contactTestBitMask = physicsCategory.Rocket
Troll.physicsBody?.affectedByGravity = false
Troll.physicsBody?.dynamic = true
}
_ = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("spawnTrolls"), userInfo: nil, repeats: true)
来源:https://stackoverflow.com/questions/37088680/attemped-to-add-a-sknode-which-already-has-a-parent-error-when-trying-to-spawn