在SpriteKit(SKCropNode)中为蒙版图像设置动画

时间:2013-10-08 20:39:04

标签: animation ios7 sprite-kit skcropnode

我正在使用Sprite Kit创建我的第一个iOS应用程序,而且我在看似非常基本的问题上磕磕绊。

我正在尝试为SKCropNode(fillNode)的内容(cropNode)制作动画。下面的代码很好地设置了SKScene(我看到插图被正确掩盖,放在正确位置的背景图像上)......

SKSpriteNode *logoBG = [[SKSpriteNode alloc] initWithImageNamed:logoBackground];
logoBG.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:logoBG];

SKCropNode *cropNode = [[SKCropNode alloc] init];
cropNode.maskNode = [[SKSpriteNode alloc] initWithImageNamed:logoMask];
SKSpriteNode *fillNode = [[SKSpriteNode alloc] initWithImageNamed:logoImage];

[cropNode addChild:fillNode];
fillNode.position = CGPointMake(0,300);
cropNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
self.logoContent = cropNode;
[self addChild:self.logoContent];

...但是我无法访问蒙面插图来设置其位置的动画(我希望它从蒙版外面开始并滑入到位置):

CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;

SKAction *moveUp = [SKAction moveByX: 0 y: 100 duration: 5];
SKAction *moveSequence = [SKAction sequence:@[moveUp]];
[self.logoContent.target_illustration runAction: moveSequence completion:^{ // '.target_illustration' = the SKSpriteNode that I want to animate; cropNode.fillNode
    SKScene *titleScreen  = [[TitleScreen alloc] initWithSize:self.size];
    SKTransition *fade = [SKTransition fadeWithDuration:(1)];
    [self.view presentScene:titleScreen transition:fade];
}];

我只能为整个logoContent而不是其子节点设置动画。

我出错的任何想法?

2 个答案:

答案 0 :(得分:5)

仅供参考,如果有人想知道如何掩盖,这是一个简单的片段

- (void) cropNodes
{
    // the parent node i will add to screen
    SKSpriteNode *picFrame = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(100, 100)];
    picFrame.position = CGPointMake(200, 200);

    // the part I want to run action on
    SKSpriteNode *pic = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
    pic.name = @"PictureNode";

    SKSpriteNode *mask = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(80, 50)];

    SKCropNode *cropNode = [SKCropNode node];
    [cropNode addChild:pic];
    [cropNode setMaskNode:mask];
    [picFrame addChild:cropNode];
    [self addChild:picFrame];

    // run action in this scope
        //[pic runAction:[SKAction moveBy:CGVectorMake(30, 30) duration:10]];

    // outside scope - pass its parent
   [self moveTheThing:cropNode];
}

- (void) moveTheThing:(SKNode *) theParent
{
    // the child i want to move
    SKSpriteNode *moveThisThing = (SKSpriteNode *)[theParent   childNodeWithName:@"PictureNode"];
    [moveThisThing runAction:[SKAction moveBy:CGVectorMake(30, 30) duration:10]];
}

答案 1 :(得分:2)

知道了!

[self.logoContent.target_illustration runAction: moveSequence completion:^{

应该是

[self.logoContent.children[0] runAction: moveSequence completion:^{

我的插图现在按照指示移动:)