Search code examples
flashactionscriptairflashdevelopflash-builder

how to make SIMPLE animated sprite actionscript?


I cant find any well explained SIMPLE way of creating animated sprite. Lets say I have 2 .png files to appear rabbit running:

rabbit1.png and rabbit2.png

 [Embed(source='assets/rabbit1.png')] public static const R1: Class;
 [Embed(source='assets/rabbit2.png')] public static const R2: Class;

i know i can create sprite and add to it frames like this:

var rabbit1:Bitmap = new R1();
var rabbit2:Bitmap = new R2();

var bunny:Sprite = new Sprite();
bunny.addChildAt(rabbit1,1);
bunny.addChildAt(rabbit2,2);

That is OK, but how to animate it in SIMPLE way ? (withour creating extra xml file or like)

Thanks!


Solution

  • Ideally you would create a class, lets say, 'SimpleAnimation', but if you don't want to there is another way (using your code)

    var rabbit1:Bitmap = new R1();
    var rabbit2:Bitmap = new R2();
    
    var bunny:Sprite = new Sprite();
    var currFrame:int = 0;
    bunny.addChildAt(rabbit1,0);
    bunny.addChildAt(rabbit2,1);
    bunny.getChildAt(1).visible = false;
    
    bunny.addEventListener(Event:ENTER_FRAME, onBunnyFrame);
    function onBunnyFrame(e:Event):void
    {
        var maxFrame:int = bunny.numChildren;
        bunny.getChildAt(currFrame).visible = false;
        currFrame++;
        currFrame = currFrame % maxFrame;
        bunny.getChildAt(currFrame).visible = true;
    }