Change Texture In Scene Editor Programmatically

182 views Asked by At

I've added a color sprite, named "arm" to my Scene Editor but for a bunch of reasons I need to access that color sprite and change the texture programmatically. Another way of stating my goal, I want to link the node to my GameScene swift file and change texture using a texture from an array. Does anyone know how to do this?

1

There are 1 answers

3
JohnL On BEST ANSWER

First things first, you want to link the sprite node from your GameScene.sks file to your GameScene.swift file.

To do this, in GameScene.swift near the top (under the "class GameScene: SKScene {") use this:

var armNode: SKSpriteNode?

In your didMove(to view:) section do this:

armNode = childNode(withName: "arm") as? SKSpriteNode

Now 3 different ways to change the color/texture of the SKSpriteNode depending on what you are doing:

armNode.colour = UIColor.red

armNode.texture = SKTexture(imageNamed: "your arm image here")

armNode.texture = textureArray[2]

The 3rd version uses a texture from an array of textures