SKSpriteNode动画渐变

时间:2013-11-05 00:53:10

标签: ios gradient sprite-kit cagradientlayer

我有一个开始透明的SKSpriteNode,我想让它从一侧开始完全可见并移动到另一侧,以整个精灵可见结束。 This question在UIViews上解决了CALayers的问题,但似乎我们无法访问SKSpriteNode的CALayers。

有没有办法将动画渐变应用于SKSpriteNodes?

1 个答案:

答案 0 :(得分:3)

以下是如何使用裁剪节点执行此操作的示例。您可以通过在Xcode中创建一个新的“SpriteKit Game”项目并替换MyScene.m中的touchesBegan来运行它:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];

        SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];

        SKCropNode *cropNode = [SKCropNode node];

        // maskNode is twice as big as the sprite:
        // fully masked on the left, fully visible on the right
        SKNode *masked = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:sprite.frame.size];
        SKNode *shown  = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:sprite.frame.size];
        shown.position = CGPointMake(sprite.frame.size.width, 0);

        SKNode *maskNode = [SKNode node];
        [maskNode addChild:masked];
        [maskNode addChild:shown];

        cropNode.maskNode = maskNode;
        cropNode.position = location;

        [self addChild:cropNode];
        [cropNode addChild:sprite];

        SKAction *fadeIn = [SKAction moveBy:CGVectorMake(-sprite.frame.size.width, 0) duration:5];
        [maskNode runAction:fadeIn];
    }
}