FillColor meets FillTexture: Changing color of named child

In the final challenge of Chapter 11, in which we are asked to change the fill color and see it blend with the Texture, I find that when I do this, the arrow’s texture does indeed blend with the Texture background. However, the SKSpriteNode that was created in the level’s SKS scene file, which was set in code to have a fillColor of clear also changes to the new fillColor. I tried creating a reference to the arrow child by giving it a name of ‘arrow’ and then changing it in interact(), however, the original SKSpriteNode also changes color.

  1. Am I doing this wrong?
  2. Why is it that I cannot create an HintNode object in GameViewController and add it as a child unless there is an SKSpriteNode in the SKS file for the level?

CODE:
In HintNode

override func touchesEnded(_ touches: Set, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
interact()
}

func interact() {
    color = colors[Int.random(min: 0, max: 2)]
    let node = childNode(withName: "arrow") as! SKShapeNode
    node.fillColor = color
    
}

Thanks!

Update:
A simple thought not elegant way to hide the placeholder SKSpriteNode is to set the size to 0.

I think that if I want to create a HintNode directly in game scene programatically without using a placeholder, I would need to add a custom initializer to the HintNode class. I’m not sure, so hopefully, one of the more experienced folks can answer :grinning:

Custom SKSpriteNode initializer is introduced in Chapter 12 … so look ahead and if you’ve come across this post for a similar reason, you can see how to modify HintNode so that it does not require a pre-existing SKSpriteNode.

@justinjngan Do you still have issues with this?

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