Dino Defense addNodes to Layer

Dino Defense Entities and Components

this is a Dictionary of type [enum: SKnode]

gameLayerNodes[.Hud]!.addChild(towerSelectorNode)

My question is How can a Dictionary use the addChild property here. Were is the trick?

The book says: You add the tower selector nodes to the .Hud game layer.

@hcastellanos22

Try putting this bit of code into a playground:

enum state {
  case begin, end
}
let nodes = [state.begin : "This is the beginning state",
             state.end : "This is the end state"]
nodes[.begin]

Here you’ve created an enum and a dictionary. You can use the state’s values to recover values from a dictionary.

nodes[.begin] will have the value "This is the beginning state". You’ve used .begin as an index (or key) into the dictionary. So nodes[.begin] is actually of type String.

Similarly, gameLayerNodes[.Hud] is of type SKNode, and you’re using .Hud as a key for the dictionary value.

The code gameLayerNodes[.Hud]!.addChild(towerSelectedNode is equivalent to:

let node = gameLayerNodes[.Hud]!
node.addChild(towerSelectedNode)

This topic was automatically closed after 166 days. New replies are no longer allowed.