2

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

Gulvan
  • 305
  • 2
  • 12
  • I don't think there is such thing as "swf animation", are you referring to a movieclip stored in a swf file? If so i believe you can use the method in this answer: https://stackoverflow.com/a/4121526/2670415 – npretto Aug 21 '17 at 22:16
  • format library or openfl swf varient, can be used to read and parse swf byte code to structures such as format.SWF that OpenFL ( when targetting html5 ) will convert into javascript similar to how Pixi 4 replicates flash structures. So I think 'swf animation' is perfectly acceptable description, since the Haxe javascript would accept a swf file with animation(s) bytecodes inside. I have provided general code approaches in my other answer. To be clear the user is not attempting to embedding a swf in html, but parsing a swf in haxe javascript your answer skips these details so unlikely useful? – Justinfront Aug 22 '17 at 00:50
  • @npretto, I am referring to SWF as a library of movieclips. I'm using swflib. To connect the library to the project, I'm using the tag with preload and generate arguments set to true, so when I need an asset, I just making a new object of its class, like this: `var redSquare:RedSquare = new RedSquare(); addChild(redSquare); removeChild(redSquare);` I'm going to read the answer though and try to use the method in my code – Gulvan Aug 22 '17 at 12:46
  • @npretto, thanks for the link, the answer you sent and the answer that was left below by Justinfront solved my problem well – Gulvan Aug 22 '17 at 13:08

1 Answers1

1

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.

Justinfront
  • 472
  • 2
  • 10
  • OK, thanks for the answer, controlling the animation with its MovieClip methods seems right solution. And checking the equality of totalFrames and currentFrame is an interesting method to play an animation exactly 1 time – Gulvan Aug 22 '17 at 12:59