我有一个开始透明的SKSpriteNode,我想让它从一侧开始完全可见并移动到另一侧,以整个精灵可见结束。 This question在UIViews上解决了CALayers的问题,但似乎我们无法访问SKSpriteNode的CALayers。
有没有办法将动画渐变应用于SKSpriteNodes?
答案 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];
}
}