0
votes

Apple recommends to use one SKTexture object if it will be used multiple times (https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKTexture_Ref/).

So I wanted to test if it is really passed as reference:

    var fireSpriteTexture:SKTexture = SKTexture(imageNamed: "fire")
    let fireSprite = SKSpriteNode(texture: fireSpriteTexture)
    fireSprite.position = CGPointMake(50,50)
    fireSprite.zPosition = 100

    // Change texture -> We should see no fire
    fireSpriteTexture = SKTexture(imageNamed: "water")

    self.addChild(fireSprite)

In my understanding this swift code should make the water texture visible in my app BUT in fact the fire texture is placed on the screen.

Why? Is it a bug?

To make it more clear I would also like to know what happens if I init my node with SKSpriteNode(imageNamed: "fire") and then copy it with its copy() method, do both objects share the same "fire" resource?

1

1 Answers

2
votes

I do not think you are understanding pass by reference correctly, you pass the reference of fire into your sprite, the sprite will then retain a reference of this texture.

You are changing the fire texture to a new texture with a new reference. This does not mean the sprite with the old reference is going to automatically change to the new reference.

If you want to check for reference, you want to compare the fire texture to the sprite texture, not create a whole new texture.

If you want to change the texture, then you need to edit the data of the texture without creating a new reference.