How do you change the color of all of the nodes of a specific name in SpriteKit with Swift

耗尽温柔 提交于 2019-12-09 04:11:35

I think you should change the color of the node, not of some block variable. You will retrieve all SKNodes with that name. But those nodes don't have a color property. Only some subclasses, for example the SKSpriteNode have a color property. You therefore have to add some additional logic to change the color. For example you could try to convert the node to SKSpriteNode and only then change the color:

self.enumerateChildNodesWithName("blocks", usingBlock: { node, stop in
    if let sprite = node as? SKSpriteNode {
        sprite.color = UIColor.orangeColor()
    }
})

As @appzYourLife correctly mentioned that code can be simplified / swiftyfied to

self.enumerateChildNodesWithName("blocks") { node, stop in
    if let sprite = node as? SKSpriteNode {
        sprite.color = .orangeColor()
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!