“Attemped to add a SKNode which already has a parent” error when trying to spawn a node

南楼画角 提交于 2019-12-12 00:36:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!