简体   繁体   中英

Why do I keep getting this error when I try to sequence my actions in Swift?

This is the error I get. I have a sequence that Im using so I could call the actions in order. I put the function addGameBall() in a runBlock so I could have that action finish last. Is that the correct way to do that. Here is the code I have: What am I doing wrong? Thanks!

Attemped to add a SKNode which already has a parent

    //RightSide
    if firstBody.categoryBitMask == GameBallCategory && fourthBody.categoryBitMask == WallCategpory {
        println("GoalRight")

        let waitBall = SKAction.waitForDuration(1.0)
        let removeFromParent = SKAction.removeFromParent()
        let respawnBall = SKAction.runBlock(self.addGameBall)
        let sequenceThis = SKAction.sequence([waitBall, removeFromParent, respawnBall])

        runAction(sequenceThis)

    }

"Attempting to add a sknode which already has a parent" means you're adding a node twice. I can't see your addGameBall code but i'm pretty confident you have a line in their that says. self.addChild(ball)//or whatever your code is called .Everytime this functions runs, the line is executed so the same reference to the ball node is added multiple times which is why the compiler is complaining. The problem could be solved by declaring ball as a local variable so that when the function runs, a new reference to the node is being created. Happy coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM