Created
November 19, 2012 18:31
-
-
Save ruby0x1/4112572 to your computer and use it in GitHub Desktop.
JSON TexturePacker importer for Haxenme haxelib SpriteSheet, with behavior and examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.eclecticdesignstudio.spritesheet.AnimatedSprite; | |
//... | |
//Load assets | |
var sprite_json = ApplicationMain.getAsset( "assets/playerSheetFromTexturePacker.json" ); | |
var sprite_behavior = ApplicationMain.getAsset( "assets/player_behavior.json" ); | |
//Load the sprite sheet and create animated sprite | |
var sprite_sheet = TexturePackerJSON.parse( sprite_json, sprite_behavior, "assets" ); | |
var player : AnimatedSprite = new AnimatedSprite( sprite_sheet ); | |
//Show animation | |
player.showBehavior ("idle"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"idle" : { | |
"frames":[ 1,1,1, 2, 3, 3, 2, 1,1,1,1 ], | |
"speed": 10, | |
"loop" : true | |
}, | |
"walk" : { | |
"frames":[ 5, 6 , 5, 7 ], | |
"loop": true, | |
"speed": 8 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.underscorediscovery; | |
import com.eclecticdesignstudio.spritesheet.data.SpriteSheetFrame; | |
import com.eclecticdesignstudio.spritesheet.SpriteSheet; | |
import com.eclecticdesignstudio.spritesheet.data.BehaviorData; | |
import hxjson2.JSON; | |
class TexturePackerJSON { | |
public static function parse(data:String, data2:String, assetDirectory:String="", name:String=""):SpriteSheet | |
{ | |
if(data == null) { | |
// trace("WARNING : NULL DATA passed into TexturePackerJSON.parse from " + assetDirectory); | |
return null; | |
} | |
var json:Dynamic = hxjson2.JSON.parse(data); | |
var bjson:Dynamic = hxjson2.JSON.parse(data2); | |
var frames:Array<SpriteSheetFrame> = new Array<SpriteSheetFrame>(); | |
var behaviors:Hash<BehaviorData> = new Hash<BehaviorData>(); | |
var behaviorFrames:Array<Int> = new Array<Int>(); | |
if (name == "") name = json.meta.image; | |
var fields = Reflect.fields(json.frames); | |
// trace("Parsing Json for " + name + ', which has ' + fields.length + ' frames.'); | |
fields.sort( function(f:String, a:String):Int{ | |
if(f < a) return -1; | |
if(f > a) return 1; | |
return 0; | |
}); | |
for (prop in fields) { | |
var frame = Reflect.field(json.frames, prop); | |
// trace( "\tadding frame : " + prop); | |
frames.push( | |
new SpriteSheetFrame( | |
Std.parseInt(frame.frame.x), | |
Std.parseInt(frame.frame.y), | |
Std.parseInt(frame.frame.w), | |
Std.parseInt(frame.frame.h), | |
Std.parseInt(frame.spriteSourceSize.x), | |
Std.parseInt(frame.spriteSourceSize.y) | |
) //spriteSheetFrame | |
); //push | |
} | |
//Now parse the behavior information, adding each behavior we find in the file | |
var bfields = Reflect.fields(bjson); | |
var count : Int = 0; | |
for (prop in bfields) { | |
var frame = Reflect.field(bjson, prop); | |
var framelist : Array<Int>; | |
framelist = frame.frames; | |
var finalframelist : Array<Int> = new Array<Int>(); | |
for(f in framelist) { | |
finalframelist.push(f-1); | |
} | |
// trace ("adding frameset " + prop + finalframelist); | |
//name:String, frames:Array <Int>, loop:Bool = false, frameRate:Int = 30, originX:Float = 0, originY:Float = 0 | |
var behaviorData : BehaviorData = new BehaviorData(prop, finalframelist, frame.loop, frame.speed, 0, 0); | |
behaviors.set(prop, behaviorData); | |
} | |
var spritesheet:SpriteSheet = new SpriteSheet(frames, behaviors); | |
spritesheet.name = name; | |
spritesheet.setImage(ApplicationMain.getAsset(assetDirectory + '/' + json.meta.image)); | |
return spritesheet; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment