Integrating Spine with SpriteKit Tutorial

Learn how to add Spine skeletal animations into Sprite Kit in this short tutorial. By Ray Wenderlich.

Leave a rating/review
Save for later
Share
You are currently viewing page 2 of 2 of this article. Click here to view the first page.

Changing Animations

Like I said, this SpriteKit-Spine library is still unofficial and in the early stages, and it seems to be missing (as far as I can tell) a bunch of handy functionality you’d typically want to use, such as the ability to change the animation to something else after you start a node.

Luckily, this is fairly easy to hack in. Open spine-spritekit\SpriteKit\DZSpineSceneBuilder.h and add the following method:

- (void)runAnimationName:(NSString *)animationName skeleton:(SpineSkeleton *)skeleton loop:(BOOL)loop;

Then open DZSpineSceneBuilder.m and implement the method as follows:

- (void)runAnimationName:(NSString *)animationName skeleton:(SpineSkeleton *)skeleton loop:(BOOL)loop {

    SpineAnimation *animation = [skeleton animationWithName:animationName];
    if (!animation) {
        NSLog(@"No such animation: %@", animation.name);
        return;
    }
    
    DZSpineSpriteKitAnimation *skAnimation = [[DZSpineSpriteKitAnimation alloc] initWithSkeleton:skeleton maps:self.maps];

    // Bone Animations
    //[skAnimation chainAnimations:[animations copy] rootBone:bone rootNode:root loop:loop];
    [skAnimation applyBoneAnimations:@[animation] loop:loop];

    // Slot Animations
    [skAnimation applySlotAnimations:@[animation] loop:loop];
}

This is a helper method to make the sprite run a different animation. To use it, open MyScene.m and add the following:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [_builder runAnimationName:@"standing" skeleton:_skeleton loop:NO];
    
}

Build and run, and now when you tap the sprite performs the standing animation.

Standing Animation

Where To Go From Here?

Here is the finished example project from the above tutorial.

At this point, you can see your Spine animation working in a game. If Sprite Kit isn’t your engine of choice, you can check out one of the many other runtimes and follow a similar process.

Speaking of which – I chose Sprite Kit for this tutorial since I’m particularly interested in it (we recently wrote a book on the subject), but if you’d like a similar tutorial on integrating Spine with another game framework, let me know.

I hope this has got you excited about the possibilities of using Spine in your game. If you have any questions or comments, please join the forum discussion below!