Does anyone know if there is a way to fade (over time) between two different SKTexture
s on an SKSpriteNode
. I am assuming that you can't do this directly and plan to use a duplicate child sprite with a higher ZPosition
to realise the fade, but I just wanted to check that there was not some method using SKAction
(s) that I had over looked.
Asked
Active
Viewed 1,266 times
3

fuzzygoat
- 26,573
- 48
- 165
- 294
-
2yup, use two sprites and cross-fade them – CodeSmile Apr 11 '14 at 09:18
1 Answers
2
The following code should address this issue assuming the new texture fits overtop of the old one (it doesn't fade out the previous texture, but simply fades in the new one on top). I've left out minor implementation details such as timing mode.
-(void) fadeTexture:(SKTexture *)newTexture ontoSpriteNode:(SKSpriteNode *)referenceSpriteNode withDuration:(CFTimeInterval)duration {
SKSpriteNode * fadeInSprite = [self fadeInSpriteWithTexture:newTexture referenceSpriteNode:referenceSpriteNode];
[[referenceSpriteNode parent] addChild:fadeInSprite];
[fadeInSprite runAction:[SKAction sequence:@[
[SKAction fadeAlphaTo:1 duration:duration],
[SKAction runBlock:^{
[fadeInSprite removeFromParent];
[referenceSpriteNode setTexture:newTexture];
}]
]]];
}
-(SKSpriteNode *) fadeInSpriteWithTexture:(SKTexture *)newTexture referenceSpriteNode:(SKSpriteNode *)referenceSpriteNode {
SKSpriteNode * fadeInSprite = [SKSpriteNode spriteNodeWithTexture:newTexture size:[referenceSpriteNode size]];
[fadeInSprite setAlpha:0];
[fadeInSprite setAnchorPoint:[referenceSpriteNode anchorPoint]];
[fadeInSprite setPosition:[referenceSpriteNode position]];
return fadeInSprite;
}

dave
- 1,150
- 1
- 13
- 22