19

i'm new to sprite Kit and having an issue with changing a current SKSpriteNode image.

My spriteNode looks like this

mover = [SKSpriteNode spriteNodeWithTexture:Texture1];
[mover setScale:1.0];
[self addChild:mover];

then i have this method that should change the mover image, but it is not. What am i doing wrong?

- (void)didBeginContact:(SKPhysicsContact *)contact {
    if (contact.bodyA.categoryBitMask == worldCategory) {
        SKTexture* explodeTexture1 = [SKTexture textureWithImageNamed:@"explode"];
        explodeTexture1.filteringMode = SKTextureFilteringNearest;

        mover = [SKSpriteNode spriteNodeWithTexture:explodeTexture1];
    }
}
Yannick Loriot
  • 7,107
  • 2
  • 33
  • 56
user3423384
  • 667
  • 2
  • 8
  • 18

3 Answers3

30

You have to change the texture property of your mover object.

Something like this:

mover.texture = [SKTexture textureWithImageNamed:@"explode"];
sangony
  • 11,636
  • 4
  • 39
  • 55
8

This method is actually re-creating the the mover object.

mover = [SKSpriteNode spriteNodeWithTexture:explodeTexture1];

You just need to update the texture with:

mover.texture = explodeTexture1;
Callum Boddy
  • 407
  • 2
  • 5
7

Swift version:

mover.texture = SKTexture.textureWithImageNamed("explode")

Swift version 3.0:

mover.texture = SKTexture(imageNamed: "explode")
Parth Adroja
  • 13,198
  • 5
  • 37
  • 71
Oscar Falmer
  • 1,771
  • 1
  • 24
  • 38