Created
April 27, 2016 06:03
-
-
Save YuukiOgino/ab8ba14aa3115ed88ce5255dd59e91f4 to your computer and use it in GitHub Desktop.
Flash(Stage3D)でSpineを動かしてみる ref: http://qiita.com/YuukiOgino/items/b146e9a70989adf25ac7
This file contains 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
/** | |
* Spineキャラクターコンポーネント | |
* | |
* @author ogino | |
*/ | |
public class SpineCharacterComponent extends Sprite | |
{ | |
//----------------------------------------------------- | |
//コンポーネント | |
//----------------------------------------------------- | |
/** スケルトンアニメーション */ | |
private var skeleton:SkeletonAnimation; | |
/** テクスチャー画像 */ | |
private var image:Bitmap; | |
/** テクスチャー */ | |
private var texture:Texture; | |
/** アタッチメントローダー */ | |
private var attachmentLoader:AttachmentLoader; | |
//----------------------------------------------------- | |
//コンストラクタ | |
//----------------------------------------------------- | |
/** | |
* コンストラクタ | |
*/ | |
public function SpineCharacterComponent() | |
{ | |
loadSpine(); | |
} | |
//----------------------------------------------------- | |
//メソッド | |
//----------------------------------------------------- | |
/** | |
* Spineデータをロード | |
*/ | |
public function loadSpine():void | |
{ | |
var loader:Loader = new Loader(); | |
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, textureLoadComplete); | |
var assetUrl:String = "テクスチャーファイルのURL(pngファイル)"; | |
loader.load(new URLRequest(assetUrl), new LoaderContext()); | |
} | |
/** | |
* アニメーション再生 | |
* | |
* @param name | |
*/ | |
public function animationPlay(name:String):void | |
{ | |
skeleton.state.setAnimationByName(0, name, true); | |
} | |
//----------------------------------------------------- | |
//イベントハンドラー | |
//----------------------------------------------------- | |
/** | |
* テクスチャーロード完了イベントハンドラー | |
* | |
* @param e | |
*/ | |
private function textureLoadComplete(e:Event):void | |
{ | |
LoaderInfo(e.currentTarget).removeEventListener(Event.COMPLETE, textureLoadComplete); | |
// テクスチャーに変換 | |
image = Bitmap(LoaderInfo(e.currentTarget).content); | |
texture = Texture.fromBitmap(image); | |
// atlasファイルを読み込む | |
var loader:URLLoader = new URLLoader(); | |
loader.addEventListener(Event.COMPLETE, atlasLoadComplete); | |
var assetUrl:String = "atlasファイルのURL"; | |
loader.load(new URLRequest(assetUrl)); | |
} | |
/** | |
* アトラスロード完了イベントハンドラー | |
* | |
* @param e | |
*/ | |
private function atlasLoadComplete(e:Event):void | |
{ | |
URLLoader(e.currentTarget).removeEventListener(Event.COMPLETE, atlasLoadComplete); | |
var spineAtlas:Atlas = new Atlas(URLLoader(e.currentTarget).data, new StarlingTextureLoader(image)); | |
attachmentLoader = new AtlasAttachmentLoader(spineAtlas); | |
// animファイルを読み込む | |
var loader:URLLoader = new URLLoader(); | |
loader.addEventListener(Event.COMPLETE, animLoadComplete); | |
var animUrl:String = "animファイルのURL"; | |
loader.load(new URLRequest(animUrl)); | |
} | |
/** | |
* アニメーションロード完了イベントハンドラー | |
* | |
* @param e | |
*/ | |
private function animLoadComplete(e:Event):void | |
{ | |
URLLoader(e.currentTarget).removeEventListener(Event.COMPLETE, animLoadComplete); | |
var json:SkeletonJson = new SkeletonJson(attachmentLoader); | |
var skeletonData:SkeletonData = json.readSkeletonData(URLLoader(e.currentTarget).data); | |
var stateData:AnimationStateData = new AnimationStateData(skeletonData); | |
// Spineアニメーションデータをセット | |
skeleton = new SkeletonAnimation(skeletonData, false, stateData); | |
// アニメーション再生 ここではwaitという名前を指定 | |
animationPlay("wait"); | |
// Stage3DにSpine描画 | |
addChild(skeleton); | |
Starling.juggler.add(skeleton); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment