I'm sorry jhonPrescott, but I am so confused.
My original example suited my needs: I needed to display a temporary HUD menu on top of an existing SKScene via a tap event, and dismiss it to go back to the SKScene. The major issue with my code was that I would have to manipulate all the SKNodes in that menu individually several times via loop, where as I wanted to just iterate over a single temporary constant and apply instance members.
Here is the order of what I need to do to make the menu work:
Initial background loading within the main SKScene (or re-entering SKScene):
- Remove any HUD menu SKNodes existing in the scene (prevents crashing)
- Add all HUD menu SKNodes to the scene
- Hide all HUD menu SKNodes from the main SKScene
HUD menu in use (on tap event):
- Unhide all HUD menu SKNodes from main SKScene
HUD menu dismissed (on tap event):
- Hide all HUD menu SKNodes from main SKScene
That is five lines of code to perform on each SKNnode, and the HUD menu will have fifteen or more SKNodes, so naturally I wanted to loop over the elements in an array:
for node in menuElements {
node.removeFromParent() //stop crashes when navigating back to page
addChild(node) //add to scene (again)
node.isHidden = true //hide until needed (on press event)
}
Rather than something like the following, which could work out as forty five lines of code if applied to fifteen elements individually:
func addHUDMenuToScene(_ skNode : SKNode, HUD: HUDMenu)
{
for node in HUD.overlayHUDElements {
node.itemTitleLabel.removeFromParent()
skNode.addChild(node.itemTitleLabel)
node.itemTitleLabel.isHidden = true
}
}
Aside from the necessity of adding elements individually to the array, afterwards I just expected to iterate over them to apply a member instance as needed, which was the issue with AnyObject, so I turned to a protocol to try to infer the class and thus the instance members that could be applied to a temporary "node" in a loop.
I hope that clears things up. I know that you have dedicated a fair amount of effort on this matter and if need be I will do it the hard way and return to the matter later on.
I need to push on and get the some functionality to the buttons in the HUD menu, but I find that they can't access existing data in the main SKScene, with data returning as nil, even though it's present inside the SKScene. I was looking into delegates on the matter, but you mentioned "manipulation functions" and "accessors" but I cannot seem to find out more on this, what system would be best e.g. from within the temporary HUD menu if I wanted to change something on the SKScene (which would result in the HUD menu disappearing) like array having a value purged or an SKSpriteNode being rotated, what would be the best approach please?
Many thanks for your time once again.