View on GitHub

Spine-spritekit

Unofficial Spine 2D Runtime for iOS 7 SpriteKit

Download this project as a .zip file Download this project as a tar.gz file

I wrote a quick code for my own project that uses Spine 2D Skeletal Animation Tool (http://esotericsoftware.com) and iOS 7 SpriteKit for my own project. Though it does not fully support all the features of Spine 2D, it basically,

Official runtimes are here: http://esotericsoftware.com/spine-runtimes

Until the official release of SpriteKit Runtime from Esoteric software, hope it's a quick starter for your projects if you are considering use of Spine and SpriteKit at the same time.

Feel free to fork and send pull requests!

simon

Screenshot - iPhone

Usage Examples

Simple

+ (SKScene *) buildSpineboyLoopWithSize:(CGSize) size
{
    SKScene *scene = [[SKScene alloc] initWithSize:size];
    scene.scaleMode = SKSceneScaleModeAspectFill;
    scene.backgroundColor = [UIColor whiteColor];
    
    SpineSkeleton *skeleton = [DZSpineSceneBuilder loadSkeletonName:@"spineboy" scale:1];
    DZSpineSceneBuilder *builder = [DZSpineSceneBuilder builder];
    SKNode *node = [builder nodeWithSkeleton:skeleton animationNames:@[@"walk", @"jump", @"walk"] loop:YES];

    // place holder node for position adjustment
    SKNode *placeHolder = [SKNode node];
    placeHolder.position = CGPointMake(size.width /2, size.height /3);
    [placeHolder addChild:node];
    [scene addChild:placeHolder];
    
    return scene;
}

Two Skeletons

+ (SKScene *) buildComplexSceneWithSize:(CGSize) size
{
    SpineSkeleton *goblin = [DZSpineSceneBuilder loadSkeletonName:@"goblins" scale:1];
    
    // skin: "goblingirl"
    spSkeleton_setSkinByName(goblin.spineContext->skeleton, "goblingirl");
    spSkeleton_setSlotsToSetupPose(goblin.spineContext->skeleton);
    
    DZSpineSceneDescription *sceneDesc = [DZSpineSceneDescription description];
    
    NSArray *tracks = @[
                        // spineboy
                        @{ @"skeleton" : @"spineboy",
                           @"scale" : @(1),
                           @"position" : NSStringFromCGPoint(CGPointMake(-80, -100)),
                           @"animations" : @[
                                            @{@"name" : @"walk"},
                                            @{@"name" : @"walk"},
                                            @{@"name" : @"jump"},
                                            @{@"name" : @"walk"},
                                            @{@"name" : @"jump"},
                                            @{@"delayUntil" : @(8)},
                                            @{@"name" : @"jump"},
                                            @{@"name" : @"walk"}],
                           @"loop" : @(YES),
                           @"wait" : @(YES) },

                        // goblin
                        @{ @"skeleton" : goblin,
                           @"scale" : @(0.8),
                           @"position" : NSStringFromCGPoint(CGPointMake(30, -200)),
                           @"animations" : @[
                                                @{@"name" : @"walk"},
                                                @{@"name" : @"walk"},
                                                @{@"name" : @"walk"},
                                              @{@"delayUntil" : @(8)},
                                              @{@"name" : @"walk"},
                                                @{@"name" : @"walk"},
                                              ],
                           @"loop" : @(YES),
                           @"wait" : @(YES) },];
    
    
    [tracks enumerateObjectsUsingBlock:^(NSDictionary *track, NSUInteger idx, BOOL *stop) {
        [sceneDesc addTrackRaw: track];
    }];
    
    NSArray *textures = @[@{ @"skeleton" : @"spineboy",
                             @"textureName" : @"goblinhead4spineboy",
                             @"attachment" : @"head"}];
    [textures enumerateObjectsUsingBlock:^(NSDictionary *texture, NSUInteger idx, BOOL *stop) {
        [sceneDesc addCustomTextureRaw: texture];
    }];
    
    
    NSArray *nodes = [sceneDesc buildScene];
    
    SKNode *placeHolder = [SKNode node];
    placeHolder.position = CGPointMake(size.width /2, size.height /2);
    
    // Give zPosition to each character so they don't mix sprites in draw orders
    CGFloat __block zPosition = 1000;
    [nodes enumerateObjectsUsingBlock:^(SKNode *node, NSUInteger idx, BOOL *stop) {
        node.zPosition = zPosition;
        [placeHolder addChild:node];
        zPosition += 100;
    }];
    
    SKScene *scene = [[SKScene alloc] initWithSize:size];
    scene.scaleMode = SKSceneScaleModeAspectFill;
    scene.backgroundColor = [UIColor whiteColor];
    
    [scene addChild:placeHolder];
    return scene;
}