2

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

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

4

1 回答 1

3

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

-(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];
    }
}
于 2013-11-05T23:19:19.417 回答