Luiz Luiz - 1 year ago 73
Swift Question

Bouncing label in Sprite Kit

I'm building a game where a label should bounce into screen (just once) when the screen is touched (it is inside touchesBegan).

I found this extension that helped with "bouncing", but I don't know what happens that when I touch restartButton (this button was working just fine) before 3 seconds (duration's time?!?!), myLabel will just appears without any animation. Am I doing anything wrong?

Ps: restartButton has a removeAllActions(). So even if this animation was on loop (which I guess is not), it would be stopped, wouldn't it?

myLabel.position = CGPoint(x: self.frame.width / 2, y: self.frame.height)
myLabel.text = "0"


//bounces into screen (using Sprite-Kit Spring)
self. myLabel.runAction(SKAction.moveTo(CGPoint(x: self.frame.width / 2, y: self.frame.height / 1.125), duration: 3.0, delay: 0.5, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0))

When restartButton is touched, it calls this function:

func restartScene(){
gameStarted = false


is the actual game scene (things such as positions, scales, physics, etc).

Answer Source

When working in Sprite Kit, try to take note that SKScene is a member of SKNode. Calling removeAllActions only removes the actions that are applied to the screen, and calling removeAllChildren only removes the children, not the actions.

You will need to call removeAllActions on all of your children to get them stop moving.

Set up an extension on SKNode if you want to have a method for your scene to remove all children's actions.

extension SKNode
    func removeAllActionsIncludingChildren()
       self.removeAllActions() { $0.removeAllActionsIncludingChildren()}