artG artG - 16 days ago 10
iOS Question

How to make blinking button from two images objective c sprite node

I want to create blinking button from two images using sprite Kit, now I have :

resButt = [SKSpriteNode spriteNodeWithImageNamed : @"retry.png"]


I saw couple posts about how to do it using UIButton, but my question is how to do it in Sprite Kit using objective c?

Answer

The thing required is an SKAction that changes your sprite's texture (an image applied to geometry). If you mean "blink" indefinitely, that action must be repeated, and there's an action for that, too.

// create textures for the sprite 
UIImage *imageA = [UIImage imageNamed:@"retryA.png"];
UIImage *imageB = [UIImage imageNamed:@"retryB.png"];
SKTexture *textureA = [SKTexture textureWithImage:imageA];
SKTexture *textureB = [SKTexture textureWithImage:imageB];

// create an action that applies the textures in sequence
NSArray *textures = @[textureA, textureB];
SKAction *blink = [SKAction animateWithTextures:textures timePerFrame:0.5];

// create an action that repeats the texture sequence
SKAction *blinkForever = [SKAction repeatActionForever:blink];

// run it
[myButtonSprite runAction:blinkForever withKey:@"blinkForever"];

Forever is a long time, of course, so this begs the question of how to stop blinking. That's why we added the withKey: qualifier to the run action above. With it, you can refer to the action in order to stop it.

[myButtonSprite removeActionForKey:@"blinkForever"];