• Stars
    star
    121
  • Rank 293,924 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A plugin for Phaser 2 that adds Spine support

Phaser Spine

Spine plugin for Phaser, it works, somehow.. No? Submit an issue, otherwise have fun :)

Key features:

  • Spine for Phaser! :o
  • Skin support
  • Possible to combine skins
  • Mixes and fading animations
  • Support for scaled atlases

Spine Version

Please note that the current spine version is an older version, and as a result of that it will work best with Spine version 3.2. There is a newer version of spine available (Spine-ts, up-to-date with latest spine runtime) but work on that is currently halted for this library.

If you feel like helping out, you're welcome to clone the spine-ts branch of this library.

Getting Started

First you want to get a fresh copy of the plugin. You can get it from this repo or from npm, ain't that handy.

npm install @azerion/phaser-spine --save-dev

Next up you'd want to add it to your list of js sources you load into your game

<script src="path/to/phaser-spine.min.js"></script>

After adding the script to the page you can activate it by enabling the plugin:

game.add.plugin(PhaserSpine.SpinePlugin);

Usage

Like any other asset in Phaser, you need to preload the spine object you want in your game. A spine Javascript export will deliver you three files; *.atlas, *.json and *.png. The preloader expects these to be in the same folder, and when so you can just preload the json file by adding the following code to your game:

game.load.spine(
    'buddy',                        //The key used for Phaser's cache
    'assets/buddy_skeleton.json'    //The location of the spine's json file
    );

Now you can just add the spine animation to your game:

buddy = game.add.spine(
    400,        //X positon
    300,        //Y position
    'buddy'     //the key of the object in cache
);

Playing animations

Phaser-spine uses the same API as the original runtime for playing an animation, after adding the spine object to your game, you can call the following function on your object:

buddy.setAnimationByName(
    0,          //Track index
    "idle",     //Animation's name
    true        //If the animation should loop or not
);

Handling events

You can handle Spine events, just add your callback to Spine.onEvent, it's regular Phaser.Signal object. Your callback should be defined with two arguments: event index (number) and event object (Spine.Event):

spineboy.onEvent.add(function (i,e) {
    console.log('name=' + e.data.name + ' int=' + e.intValue + ' float=' + e.floatValue + ' string=' + e.stringValue);
}

Crossfading animations

Stacking animations in spine is rather simple, you just add non-looping animations on top of eachother:

spineboy.setAnimationByName(0, "jump", false);  //We'd like to show this animation once
spineboy.addAnimationByName(0, "walk", true);   //And after the previous animations is finished, we continue with this one

Now this will just simply chain the animation, and it will look a bit akward. Spine solves this by adding mixes that tell spine how to transition nicely from one animation to another. You can set mixes in phaser spine as well by doing the following:

spineboy.setMixByName("walk", "jump", 0.2); //transition from walk to jump and fade/blend it over a period of 0.2 seconds
spineboy.setMixByName("jump", "walk", 0.4); //transition from jump to walk and fade/blend it over a period of 0.4 seconds

Switching skins

Another great additions to spine is the support of skins and the ability to freely switch between them. Like animations this is simple and we use the same API as the runtime. Don't forget to call setToSetupPose after switching skins to update all attachments correctly.

buddy.setSkinByName('outfit01');
buddy.setToSetupPose();

Combining skins

Now the last final awesome part is that you can also create skins in code yourself by simply grouping other existing skins.

var newSkin = buddy.createCombinedSkin(
    'outfit02',     //The name of the new skin, will be automaticly added to the skeleton data
    'vest',         //One of the skins we want to combine
    'mask'          //The other skin we want to combine
);

//Setting the new skin can be set with:
buddy.setSkin(newSkin);
//Or by referencing the new name
buddy.setSkinByName(outfit02);

//And then we shouldn't forget to setToSetupPose ;)
buddy.setToSetupPose();

Scaled atlases

In Spine it's possible to define different scales for your export atlases, including a special suffix that will be suffixed to each atlas name. By default this plugin assumes that no scale is configured, ergo everything is set with scale=1. If you export your atlases to a smaller scale, than this will only happen to the images, the original bone structure will still be defined at the original size you defined in your spine project.

If the exported image is scaled up (or down), than this has to be inverted by the runtime in order to have the correct size of the images for the attachments. In order to do do this correctly, the suffix for a scale any other than 1 has to be set. By default this plugin parses the suffix with a regular expression that looks for numbers start with @ and ending by x. So an atlas file with a scale of 0.5 should have a suffix of @0.5x and the resultion file name would be [email protected].

If you'd like a different setup you can do so by supplying a new RegExp object to the follow property:

PhaserSpine.SpinePlugin.RESOLUTION_REGEXP = /#(.+)r/;

Now the Spine plugin will look for suffixes that look like: #0.5r

The next step is to tell the preloader which scaling variants are available for loading, by adding them as an array in the 3rd optional parameter:

game.load.spine('shooter', "shooter.json", ['@0.7x', '@0.5x']);

The preloader will load 1 json (with all the skeleton/animation data), and 2 images and 2 atlas files.

Later in your game, when you create a new spine object, you again need to add the scaling variant you would like to have for your game. This way you can conditionally load different assets. If you pass no scaling variant to the object, it will just get the first variant from the array supplied when you preload the spine object.

shooter = game.add.spine(400, 300, "shooter", '@0.7x');
//the above is equal to:
shooter = game.add.spine(400, 300, "shooter");
//because @0.7x is the first element of the array supplied to the preloader

Todo

  • adding a body for physics
  • Handling input

Credits

Credit due, where credit's due as my mom always said. This version of phaser-spine is based on the original work by Studio krok's PhaserSpine

Disclaimer

We at Azerion just love playing and creating awesome games. We aren't affiliated with Phaser.io and/or esotericsoftware. We just needed some awesome spine animations in our awesome HTML5 games. Feel free to use it for enhancing your own awesome games!

Phaser Spine is distributed under the MIT license. All 3rd party libraries and components are distributed under their respective license terms.

More Repositories

1

phaser-input

Adds input boxes to Phaser like CanvasInput, but also works for WebGL and Mobile, made for Phaser only.
TypeScript
202
star
2

phaser-ts-boilerplate

The boilerplate for Phaser 2 we use internally at Azerion
TypeScript
107
star
3

phaser-ads

A Phaser plugin for providing nice ads integration in your phaser.io game
TypeScript
87
star
4

phaser-nineslice

NineSlice plugin for Phaser!
TypeScript
54
star
5

phaser-super-storage

A cross platform storage plugin for Phaser
TypeScript
52
star
6

phaser-web-workers

A simple Phaser plugin that allows you to easily integrate Web Workers in your game
TypeScript
51
star
7

phaser-i18next

Phaser plugin for translations using i18next
TypeScript
34
star
8

phaser-responsive

Adds a set of responsive objects to Phaser that can be pinned to
TypeScript
26
star
9

h5-ad-wrapper

Advertisement provider wrapper, similar to @azerion/phaser-ads but not tied into Phaser :)
TypeScript
17
star
10

h5-cordova-starter

Starter project for Cordova HTML5 games
17
star
11

LimitlessGEM

NodeJS control for LimitlessLED wificontroller 3.0
JavaScript
11
star
12

h5-texture-unpacker

Allows you to unpack your Html 5 texture files
TypeScript
10
star
13

phaser-cachebuster

Simple Phaser plugin for adding a query parameter to assets URL's so that they can be 'cache busted'
JavaScript
6
star
14

phaser-amazon-cognito

A Phaser plugin that adds User Login/Sync support trough Amazon Cognito Identity/Sync
TypeScript
4
star
15

BlueStackSDK

C++
3
star
16

h5-platform-wrapper

Wraps your common game platform API's in to one simple library, supports @gawkbox
HTML
2
star
17

cordova-google-play-servcies

Cordova plugin for google play servcices (Highscores and achievements)
Java
2
star
18

phaser

JavaScript
1
star
19

gamedock-sdk

The official Gamedock SDK for Unity, Cordova, AIR, Android and iOS.
Objective-C
1
star
20

multi-pass

A simple javascript multivariate testing framework for use with Google Analytics
JavaScript
1
star
21

gamedock-docs

The official Gamedock Documentation and issue tracking for Unity, Cordova, Android and iOS SDK's
HTML
1
star
22

quartz-storage

Cross browser/platform safe localStorage implementation
JavaScript
1
star