htmlanimationhaxeopenflshockwave

Waiting for the end of an animation in Haxe/OpenFL


I'm making a turn-based game using Haxe + OpenFL + swf + Actuate (I'm targeting js). One of the most important gameplay aspects is using abilities. Each ability has an animation and, of course, it takes time for this animation to play.

I want to know how can I handle the end of the swf animation? Thanks in advance


Solution

  • There are various ways. If you have a movie called 'mySwf' with an MovieClip setup on the main timeline with the linkage 'holder' you can attach it and control with an Enterframe loop as per this Haxe NME example ( OpenFL pretty similar ). Not tried it with html5 as my code is NME which I often prefer over OpenFL, but unfortunately NME does not support html. You can find an alternate approach in the NyanCat sample in NME haxelib, there is likely a similar one using the 'swf' library in OpenFL sample folder, so hopefully it will be just a matter or switching imports over.

    package;
    import nme.display.Sprite;
    import nme.display.StageAlign;
    import nme.display.StageScaleMode;
    import nme.Assets;
    import format.SWF;
    import format.swf.instance.MovieClip;
    import flash.events.Event;
    class Main extends Sprite{
      public function new(){
         super ();
         loadMovie( 'mySwf');
      }
      var mc: format.swf.instance.MovieClip;
      public function loadMovie( movie: String ){
         var swf = new SWF ( Assets.getBytes ("assets/"+movie + ".swf") );
         mc  = swf.createMovieClip("holder");
         mc.gotoAndStop(0);
         mc.play();
         mc.addEventListener( Event.ENTER_FRAME, loop ,false,0,true );
         addChild( mc );
      }
      function loop( e ){
         if( mc.totalFrames == mc.currentFrame ){
            mc.stop();
            mc.removeEventListener( Event.ENTER_FRAME, loop );
         }
      }
     }
    

    Make sure you name the MovieClip you want to attach with a different name to the linkage identifier which for example code needs to be 'holder', setup Linkage to 'Export for Actionscript' and 'Export in first Frame' and place on first frame of timeline. This example above will work fine for CS3 flash swf ( as3 ) or newer, I think the NyanCat example needs newer flash version such as Animate.

    If you want to look at simpler more efficient approaches with webgl and canvas fallback you need to export your timeline as a set of png's from Flash, and then you can load the images into your application and show a different one each frame. For instance if you used Kha instead of OpenFL you could in your render loop do something like...

    function render2D( g2: kha.graphics2.Graphics ){
            g2.begin(false);
            g2.transformation = FastMatrix3.translation( dx, dy ); // or rotation etc..
            if( count < images.length ){
                g2.drawImage( images[count++], posx, posy );
            }
            g2.transformation = FastMatrix3.identity();
            g2.end();
        }
    

    Obviously you can also explore Flump which is supported in Kha, Luxe, OpenFL, NME, Flambe ( exports nested structures from fla's as png and json/xml ). Or you can look at SpriteSheet/TileSheet solutions. However Swf is nice and is supported but not used much, and might be optimal file size but I would imagine for html it's quite an overhead and may not be well supported on OpenFL Javascript target at least probably not as well as the support on C++. Also swf support for NME/OpenFL is not perfect for gradients, and actions are not supported. But a sprite sheet png would always work. Other alternatives might be to try to export frames to svg.