hide and unhide SKSprite Node

1.8k Views Asked by At

I am trying to hide a node when it's clicked and unhide it when a button is clicked. After unhiding the node, I can't click it again but I want it to be clickable again. Below is my code for hiding and unhiding using SKAction. Am I missing something?

when the ball Node is clicked it is hidden and added to an array clickedBall

ballNode.runAction(SKAction.hide())
clickedBall.append(ballNode)

when the button is clicked, it is shown via array clickedBall

clickedBall.last?.runAction(SKAction.unhide())

but I can't click the node again to hide it again..

please help.. thank you

2

There are 2 best solutions below

1
On

When the node is clicked, instead of running an action, you can use the node's 'hidden' property:

ballNode.hidden = true

and then set the value to false when you want the node to reappear

0
On

When the node is clicked, to hide it you can run ballNode.removeFromParent() and to add it back addChild(ballNode)