To tease with some exciting news, Gold Leader has been picked up for a sponsorship!
On an immediate coding topic though, this has meant dealing with something I’d been dreading: Incorporating the sponsor’s splash screen. As expected they gave me an FLA and compiled SWF. Fortunately it turned out to be just a simple movie, no buttons to hook up or anything. Quite typically for Haxe/NME/ActionScript, making this work turned out to be not muchcode, but kind of a pain to figure out.
NME, along with the SWF library, does now include some cross-platform support for playing SWFs. Josh Granick has notes here, although be sure to read the comments as the API has been updated since that post (I don’t see a particularly better update post). However, as far as I can tell, at the moment this only supports the graphics. It wouldn’t play the sound effect associated with my movie and threw an error trying to decode it (something like “Unknown sub-tag SoundStreamHead2,” where the latter begins the encoded sound chunk in the SWF).
I really only care about playing the SWF in Flash though, so happily instead I can just have Flash load and play the movie. First off, the movie gets included in the compiled program SWF as a binary asset. Simply copy the movie SWF into your assets folder, include with the assets command in your .nmml as usual, and NME will assume it’s a binary asset.
The code to then play that movie clip looks something like this:
import nme.display.Sprite; import nme.Assets; import nme.Lib; import nme.display.Loader; import nme.display.MovieClip; import nme.events.Event; class Main extends Sprite { public function new () { super (); var bytes = Assets.getBytes("assets/splash.swf"); trace("Bytes " + bytes.length); var loader:Loader = new Loader(); loader.loadBytes(bytes); loader.contentLoaderInfo.addEventListener (Event.COMPLETE, function (_) { var mc:MovieClip = cast(loader.content, MovieClip); trace("Frames: " + mc.totalFrames); mc.addFrameScript(mc.totalFrames-1, function():Void { trace("Done."); mc.stop(); removeChild(mc); }); addChild(mc); }); // end main } // end Main }
Note that you could skip the dynamic cast to MovieClip and simply add the loader object to the stage/parent directly. However, I need the movie object itself so I can detect when it has stopped and move on to the actual game.
That last point is itself somewhat interesting. Utterly shockingly, ActionScript3 has an event for absolutely everything except movie clips completing. Seemingly it’s just not there, doesn’t exist… This blew my mind.
What most people do is add a function to the ENTER_FRAME
event that checks every frame to see whether or not the current frame is the last frame, and throw another event or set a property if so. However, Flash internally can associate a script with each frame, to be executed as it plays. The seemingly undocumented function MovieClip.addFrameScript()
allows you to add a function to particular frames of the movie. In general you’d have to be careful about blowing away other code, but here I don’t have that problem—there’s nothing there, and I just want to bail. So, I add a function to the last frame of the movie to do some housekeeping, and we’re all set!
Like I said, this is all fairly simple in the code, but I haven’t seen anybody spell out how to make this work, so hopefully this will be of use.