正向和反向播放数组中的Sprite Kit动画

时间:2015-03-05 23:18:22

标签: ios objective-c arrays animation sprite-kit

我尝试使用他们自己的方法播放一组单独的动画,尽管我不知道如何在Sprite Kit中执行此操作。

我已经设置了上一个和下一个按钮:

 -(void)didMoveToView:(SKView *)view {

   ....

// Add buttons
    SKSpriteNode *previous = [SKSpriteNode   spriteNodeWithImageNamed:@"previous"];
    previous.position = CGPointMake(375, 50);
    previous.zPosition = 1;
    previous.name = @"previousButton";
    [self addChild:previous];

    SKSpriteNode *next = [SKSpriteNode spriteNodeWithImageNamed:@"next"];
    next.position = CGPointMake(670, 50);
    next.zPosition = 1;
    next.name = @"nextButton";
    [self addChild:next];
}

我自己的方法中的动画集(最终会有16个):

- (void)testAnimation1 {

    SKSpriteNode *yellowDiver = [SKSpriteNode spriteNodeWithImageNamed:@"yellow"];

    yellowDiver.xScale = .75;
    yellowDiver.yScale = .75;
    yellowDiver.position = CGPointMake(380,200);
    yellowDiver.zRotation = -M_PI / 8;

    SKAction *moveYellowDiverX = [SKAction moveToX:450 duration:1.0];
    SKAction *moveYellowDiverY = [SKAction moveToY:300 duration:1.0];
    SKAction *rotateYellowDiver = [SKAction rotateByAngle:(-M_PI / 4) duration:1.0];

    SKAction *yellowDiverSequence = [SKAction sequence:@[moveYellowDiverX, moveYellowDiverY, rotateYellowDiver]];

    [yellowDiver runAction:yellowDiverSequence];
    [self addChild:yellowDiver];
}

- (void)testAnimation2 {

    SKSpriteNode *blueDiver = [SKSpriteNode spriteNodeWithImageNamed:@"blue"];

    blueDiver.xScale = .75;
    blueDiver.yScale = .75;
    blueDiver.position = CGPointMake(620,200);
    blueDiver.zRotation = M_PI / 8;

    SKAction *moveBlueDiverX = [SKAction moveToX:550 duration:1.0];
    SKAction *moveBlueDiverY = [SKAction moveToY:300 duration:1.0];
    SKAction *rotateBlueDiver = [SKAction rotateByAngle:(M_PI / 4) duration:1.0];

    SKAction *blueDiverSequence = [SKAction sequence:@[moveBlueDiverX, moveBlueDiverY, rotateBlueDiver]];

    [blueDiver runAction:blueDiverSequence];
    [self addChild:blueDiver];
}

- (void)testAnimation3 {

    SKSpriteNode *greenDiver = [SKSpriteNode spriteNodeWithImageNamed:@"green"];

    greenDiver.xScale = .75;
    greenDiver.yScale = .75;
    greenDiver.position = CGPointMake(380,500);
    greenDiver.zRotation = M_PI / 8;

    SKAction *moveGreenDiverX = [SKAction moveToX:500 duration:1.0];
    SKAction *rotateGreenDiver = [SKAction rotateByAngle:(-M_PI) duration:.5];
    SKAction *rotateGreenDiver2 = [SKAction rotateByAngle:(-M_PI/8) duration:.5];
    SKAction *moveGreenDiverY = [SKAction moveToY:385 duration:2.0];

    SKAction *greenDiverSequence = [SKAction sequence:@[moveGreenDiverX, rotateGreenDiver, rotateGreenDiver2, moveGreenDiverY]];

    [greenDiver runAction:greenDiverSequence];
    [self addChild:greenDiver];
}

...

然后在touchesBegan方法中,我想以数组顺序调用每个动画方法,这样每次按下" next"它进入集合中的下一个动画,同时在每次新动画开始时杀死并删除上一个动画。非常像带有上一个和下一个按钮的相册。

当然,使用上一个按钮,我想做同样的事情,但反之亦然:

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

    for (UITouch *touch in touches) {
    CGPoint pointInSKScene = [self.view convertPoint:[touch locationInView:self.view] toScene:self];
    SKNode *touchedNode = [self nodeAtPoint:pointInSKScene];

    if ([touchedNode.name isEqualToString:@"previousButton"]) {

        // Play Animation Set in Reverse
        [self testAnimation8];
        [self testAnimation7];
        [self testAnimation6];
        [self testAnimation5];

        //Kill First Set then Play new set in Reverse
        [self testAnimation4];
        [self testAnimation3];
        [self testAnimation2];
        [self testAnimation1];

        //Repeat 16 times
    }

    else if ([touchedNode.name isEqualToString:@"nextButton"]) {

        // Play Animation Set
        [self testAnimation1];
        [self testAnimation2];
        [self testAnimation3];
        [self testAnimation4];

        //Kill First Set then Play new set
        [self testAnimation5];
        [self testAnimation6];
        [self testAnimation7];
        [self testAnimation8];

        //Repeat 16 times
      }
    }
  }

是否可以将这些方法存储在一个数组中,然后使用我创建的按钮在Sprite Kit中递增它们?我不认为我可以使用节点名称,因为在每种方法中都会有4个节点。我需要使用方法名称。为简洁起见,动画的示例代码很简短。

1 个答案:

答案 0 :(得分:0)

您可以使用代码块将动画或任何自定义代码分组到SKAction中,并将它们放入数组中:

            NSArray *actions = @[
               [SKAction runBlock:^{
                //action1
               }],
               [SKAction runBlock:^{
                //your code here
               }]  
            ];

然后,你可以从数组中获取一个动作,并在场景中运行它,例如。 或者代替SKAction的数组,你可以使用一组数组(参见this question