Changing SK Textures for "MouseOver" in Xcode 5.0
I have a sprite with a given texture using:
joyStickRight = [SKSpriteNode spriteNodeWithImageNamed:@"joyStick.png"];
And I'd like to change it when the user touches and holds the sprite. When I detect the touch, I try changing the sprite texture by calling the same function with a different image:
joyStickRight = [SKSpriteNode spriteNodeWithImageNamed:@"joyStick_rollOver.png"];
But this does not seem to work. Nothing changes.
This for an iPad application. I am creating the on screen elements with SKSpriteNodes.
This is likely to be related to variable scope. I'm guessing you are trying to modify another instance of 'joyStickRight' SKSPriteNode. This second instance would not have been added to the scene and therefore not taking any effect.
The only way i found out to change textures ist to swap them via action like this:
SKTexture *texture1 = [SKTexture textureWithImageNamed:@"texture1"]; SKTexture *texture2 = [SKTexture textureWithImageNamed:@"texture2"]; SKAction *swapTextures = [SKAction repeatAction:[SKAction animateWithTextures:@[texture1,texture2] timePerFrame:0.1] count:3]; [node (in your case joyStickRight) runAction:swapTextures];
I still hope this is not the only solution, and also wonder why it does not work when i run it once!!!