zondag 20 december 2015

Stencyl / HaXe : Changing Animation of Actor dynamically


== Animating an Actor ==


To make different animated characters we can create a standard actor.
For instance an enemy character.
Give it the "visible" and "invisible" animations.

The visible will have at least 8 frames in it so that we can alter the animation and still have the animation effect working without having to change too much!


The loaded PNG file (it must be PNG!) should have all the frames next to eachother:
1-2-3-4 …
The number of frames will divide the width of the PNG so to create different frames during game-play.


[+ add animation]
Name: visible
Add Frame : invisible.png
<Copy Frame> and repeat <Paste Frame> for all the frames you need!


=======================
[+ Advanced => Import]
import nme.display.BitmapData; // bitmapData
import com.stencyl.Data; // casting to the tileset
import com.stencyl.models.scene.Tileset; // we remove it
// Rest is probably not needed as they are for URL requests:
import nme.display.Loader;
import nme.net.URLRequest;

[+ Advanced -> Code Block]


var loader:Loader;
var bitmapData:BitmapData;
var abitmapData:BitmapData;

// This is the function that will be called when loading the file
public function onCompleteAnimation(event:Event){


var iti:Array<Int>=new Array<Int>();



var aa:Animation;
for(a in _theActor.sprite.animations){
aa=a;
}
aa.looping=true;

var frames=8;

var theloader=event.currentTarget.loader;
var bit:Bitmap=cast(theloader.content,Bitmap);
bitmapData=bit.bitmapData;

var aloader:Loader = event.currentTarget.loader;
var content:Bitmap = cast(aloader.content, Bitmap);
var source:BitmapData = content.bitmapData;
// for each frame => speed
for( ab in 0 ... frames ) iti.push(100);
var ba:BitmapAnimation=new BitmapAnimation(source,frames,iti,true,null);

_theActor.addAnimation("visible",ba);
_theActor.originMap.set("visible", new B2Vec2(source.width/frames,source.height));
_theActor.setAnimation("visible");

}



In the load state (clicking on scene for instance) we will load the graphics:
[ for each [actor of type] ]  (this makes index0)
[ Code Block ]
 _theActor = actorOfType;

 loader=new Loader();
 loader.load(new URLRequest("https://dl.dropboxusercontent.com/u/..."));
 _theActor.setX(32*index0);
 loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleteAnimation);

Geen opmerkingen:

Een reactie posten