swiftsprite-kitalphaaddchild

Manipulating alpha property vs adding/removing node


If I want a node or nodes (in this case a "panel" with two "buttons" - e.g.: a node to represent the panel, and two more nodes on that panel to represent buttons) to appear and be available to call actions (with the buttons using touchesBegan()), I seem to be able to do it by creating the nodes (color Sprites) in my .sks scene and using alpha = 0/1. I can also do it programatically by creating the node in .swift class and using .addChild()/.removeFromParent() functions.

Are these interchangeable approaches or is there a danger of using alpha since the nodes are still present, though not seen?


Solution

  • In Spritekit hidden and alpha properties are equal to 0 as default value.

    From Apple regarding hidden objects

    objects are not rendered. However the still exist in the scene and continue to interact with it in other ways

    So that would be the same thing as having created the object and not added it to a parent

    Having just run a test on an object that was detected in the touchesBegan func.

    When the object had .alpha = 0 it was still detected in the touchesBegan

    When the object was created but not added to the parent it was still detected in the touchesBegan

    So I think both methods are comparable

    Edit> I stand corrected they are not comparable, please feel free to unselect my answer. Please comments to see pitfalls of using alpha = 0