简体   繁体   English

前后播放数组中的Sprite Kit动画

[英]Play Sprite Kit animations in an array forward and reverse

I'm trying to play a set of separate animations that I have each in their own method although I don't know how to do this in Sprite Kit. 我正在尝试播放一组单独的动画,尽管我不知道如何在Sprite Kit中做到这一点,但每个动画都有自己的方法。

I've set up Previous and Next buttons: 我设置了上一个和下一个按钮:

 -(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];
}

And my set of animations in their own methods (eventually there will be 16): 我用自己的方法制作的动画集(最终将有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];
}

...

Then in the touchesBegan method I want to call each animation method in an array order so that every time I press "next" it goes to the next animation in the set, while killing and removing the previous animation every time a new animation starts. 然后在touchesBegan方法中,我想按数组顺序调用每个动画方法,这样,每当我按“ next”键时,它将转到该集中的下一个动画,同时每次启动新动画时都将其杀死并删除。 Very much like a photo album with previous and next buttons. 非常像带有上一个和下一个按钮的相册。

Of course with the Previous button I'd like to do the same, but in reverse: 当然,使用“上一步”按钮,我也想这样做,但是相反:

-(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
      }
    }
  }

Is it possible to store these methods in an array and then increment through them in Sprite Kit with the buttons I created? 是否可以将这些方法存储在数组中,然后使用我创建的按钮在Sprite Kit中逐个递增? I don't think I can use the node names because in each method there will be 4 nodes. 我认为我无法使用节点名称,因为在每种方法中将有4个节点。 I need to use the method names. 我需要使用方法名称。 The sample code for the animations is short for brevity. 动画的示例代码简短明了。

You can group up your animations or any custom code into SKAction s with code blocks and put them into an array: 您可以将动画或任何自定义代码SKAction到带有代码块的SKAction ,然后将它们放入数组中:

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

Then you can get an action from the array and run it against the scene, for example. 然后,例如,您可以从数组中获取一个动作并对其进行操作。 Or instead of an array of SKAction s you could use an array of blocks (see this question ) 或者,您可以使用一个块数组来代替SKAction的数组(请参阅此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM