Dante Hoyte
Dante Hoyte

Reputation: 333

run SKAction that follows path while updating animations

I'm using sprite kit. I need my characters to move along a path while updating animations. For example, I want to change my run animation to a left facing run animation when my skspritenode starts moving left.

I've tried many combinations of sequences, run blocks, etc, but nothing is working. Any suggestions. Pseudocode below:

//run initial run animation
//get array of points to move to
//load skactions into an array 
   //upon completion of an skaction re-evaluate which animation to use.
//repeat

//run sequence of SKActions

Thanks for any help.

Upvotes: 0

Views: 203

Answers (1)

Dante Hoyte
Dante Hoyte

Reputation: 333

Ok I figured something out. To clarify what I wanted, I wanted to take a series of points in an array and run them through a function that would move my skspritenode and run animations on it corresponding to the directions my character needed to run. Here's what I did:

//i and j are int class variables here, don't ask why I had two, but it worked so I didn't change it.

    for (;i<len;i++) {
        [actions_array addObject:[SKAction moveTo:CGPointMake(apath[i].x, apath[i].y) duration:1.0]];
        [actions_array addObject:[SKAction runBlock:^(void){
            j++;
//remove the previous action just in case
                [self removeActionForKey:[NSString stringWithFormat:@"%d",j-1]];
                NSArray* frames = [self returnAnimForMoveToPt:CGPointMake(path_to_goal[j+1].x, path_to_goal[j+1].y)];
                [self runAction:[SKAction repeatActionForever:[SKAction animateWithTextures:frames timePerFrame:1.0/30.0]] withKey:[NSString stringWithFormat:@"%d",j]];
            }]];
            }

    [self run

Action:[SKAction sequence:actions_array] completion:^(void){
                //******movement complete, at location*******!!!!!!!!
                [self goIdle];
//other completion handl
            }];

Essentially I just run move instructions followed by runBlocks with animations repeatedly. Hope this helps.

Upvotes: 0

Related Questions