Don P
Don P

Reputation: 63667

Add a texture to an existing SKSpriteNode in Swift

How can I add a texture to an existing SKSpriteNode?

In the docs all I see is a way to initialize a SKNode with a texture, but not a way to add a texture to it.

// Creating with a texture is easy.
var mySprite = SKSpriteNode(texture: myTexture)

// But no method for adding a texture.
var anotherSprite = SKSpriteNode()
anotherSprite.texture = myTexture // This is not valid.

Upvotes: 1

Views: 1313

Answers (1)

Kazuki Sakamoto
Kazuki Sakamoto

Reputation: 13999

You need to update the size of the sprite.

var anotherSprite = SKSpriteNode()
anotherSprite.texture = myTexture
anotherSprite.size = myTexture.size()

It seems SKSpriteNode() initialized the size of the sprite as width:0, height:0. So you can't see the sprite in the scene.

Upvotes: 5

Related Questions