1

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?

punated
  • 51
  • 6

1 Answers1

1

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

Ron Myschuk
  • 6,011
  • 2
  • 20
  • 32
  • Thanks Ron. Can that cause problems? How does an object with zero alpha interact in other ways? I thought that if alpha = zero, it may be there, but it can't be interacted with. – punated Apr 23 '17 at 19:53
  • Other ways would be...You can detect position, check if it overlaps with other nodes and check it's properties – Ron Myschuk Apr 23 '17 at 19:55
  • OK, I appreciate that Ron. I think I get it now. – punated Apr 23 '17 at 20:00
  • The alpha is just a color component. Setting the Alpha to 0 is pretty much the same as setting the color to red if you see what I mean. On the sprite level, the alpha property is used internally by spritekit to modify the color of every pixel in order to blend them, but making it invisible that way does not take it out of the rendering loop. – BadgerBadger Apr 23 '17 at 22:44
  • @BadgerBadger having an item with an alpha of 0 is not the same as an object being red. A red object will get rendered where as an alpha zeroed node will not. Please see my quote from Apple in my answer. – Ron Myschuk Apr 23 '17 at 23:11
  • @BadgerBadger if you have one item in your scene and put on showsDrawCount in the GameViewController you will see that draws = 1. If you change that one item to alpha = 0 the draws = 0 – Ron Myschuk Apr 23 '17 at 23:25
  • They are not the same thing, Hidden objects are still part of scene tree, which means it will slow down searches, and be affected by physics or anything else that can be affected by the scene tree – Knight0fDragon Apr 24 '17 at 19:38
  • Also, Not sure you did your touchesBegan test correctly, an item with an alpha of 0 gets ignored by touchesBegan (Unless they changed this again) – Knight0fDragon Apr 24 '17 at 19:39
  • @Knight0fDragon I just tested it again, it definitely gets fired when I touch it in `touchesBegan` and `alpha = 0`. I'm using `node.contains(touchLocation)` – Ron Myschuk Apr 24 '17 at 19:54
  • yes, that means your test is incorrect, Your touch event is fired on the scene, not on the node. You are then grabbing any node on the scene at a particular point, all that function does is check the frame of the node, it does not check for alpha or hidden – Knight0fDragon Apr 24 '17 at 19:55
  • Sub class the node, override the touch events on it, then do your test, if scene gets called instead of node, then you know touch events on the node get ignored when hidden – Knight0fDragon Apr 24 '17 at 19:56
  • @Knight0fDragon fair enough, but I also did only originally say that the object was "detected in the touchesBegan", and the OP doesn't state that they subclassed the nodes. – Ron Myschuk Apr 24 '17 at 20:11
  • Your answer does not "detect" in the touchesbegan function (you require further steps), and it wasn't till your test that we learn it is detected in the node.contains method you are referring to is where you do your work, which is not unique to touches began and can be done at any point in time, not just when a touch happens. Either way it doesn't matter because unfortunately this is not an answer to the OP question. – Knight0fDragon Apr 24 '17 at 23:50
  • They are not interchangeable and the danger is longer search times, physics bodies attached to nodes would still exist and in your case, a node.contains would return 2 different results, true when using alpha is 0, false when node is not attached to scene. Not trying to be a jerk, just trying to help you understand there is a difference. – Knight0fDragon Apr 24 '17 at 23:51