Prashanth
Prashanth

Reputation: 548

addChild not working in SpriteKit

I'm developing a game similar to: https://itunes.apple.com/us/app/impossible-rush-hd/id964396464?mt=8

An array of ball objects ( Ball: SKNode ) with different colors is created. And every time ball touches the wheel I'm removing the ball using:

ball.removeFromParent() 

This is working fine but after that I pick another random ball to display from the array and add to the scene it doesn't work. Node count shows 1 which is the Wheel node.

I call this function initially and it works fine but doesn't work the next time when called from the didBeginContact method.

func sendBall(){
     var randomIndex = Int(arc4random_uniform(UInt32(self.ballsArray.count)))
     ball = self.ballsArray[randomIndex]
     addChild(ball) //This statement has no effect when its called second time.
}

I'm stuck here. Can someone please help me.

Upvotes: 0

Views: 629

Answers (1)

jpulikkottil
jpulikkottil

Reputation: 686

but doesn't work the next time when called from the didBeginContact method.

We can't add child inside the didBeginContact, do it from didSimulatePhysics method

Set a flag in didBeginContact() and in didSimulatePhysics() check flag and call sendBall() and reset the flag :)

Upvotes: 1

Related Questions