Hi Joshua,
I think I have the perfect solution for this. I haven’t tested this code and some of it might not be 100% valid haxe code, but I think it’s totally doable.
Only a couple very minor additions to the MovieClip class are necessary:
private var frameScripts:Map<Int, Function>;
And these two lines inside stage_onEnterFrame’s for loop:
var script:Function = clip.frameScripts.get(clip.get_currentFrame());
if(script != null) script.invoke();
Here’s how you would add a script to a frame:
frameScripts.set(1, function():void {
// The code of the script goes into the anonymous function
stop();
} );
That can be written manually like so and would probably work fine, but the main purpose is to be used with the openfl flash plugin which would generate such code based on what it finds inside symbol’s timeline’s frames.
So the idea on the plugin’s side is that the plugin generates a haxe class for every symbol inside the swf exactly like the SWF library does nowadays, but also generates lines like the one above, where it assigns functions to the function map.
Here’s an example of what a class generated by the openfl flash plugin would look like (I based this on the MovieClip.mtt template and left in the keywords that need to be replaced to not lead to confusion as to how I got those values):
Here’s a link as it’s badly formatted below.
class ::CLASS_NAME:: extends MovieClip { // exactly the same as before
public function new () {
// This is the same as earlier
if (!SWF.instances.exists ("::SWF_ID::")) {
SWF.instances.set ("::SWF_ID::", new SWF (Assets.getBytes ("::SWF_ID::")));
}
var swf = SWF.instances.get ("::SWF_ID::");
var symbol = swf.data.getCharacter (::SYMBOL_ID::);
super (cast symbol);
}
// Now here's where the frameScript map population would happen. All the code below is
// generated by the openfl flash plugin
// frames 1, 17 and 29 are random values. Real values would be determined by the openfl
// flash plugin based on which frames actually have code on them
frameScripts.set(1, function():void {
// The code inside the anonymous function is literally a copied string of text that was
// in the frame inside Flash Professional. I think that the string of the frame's content
// can be gotten by the getScript() function in the SDK.
stop();
} );
frameScripts.set(17, function():void {
// Maybe this frame has some different code
goToAndStop(1);
} );
frameScripts.set(29, function():void {
// Maybe this frame has some haxe code that isn't valid actionscript code
if(FileSystem.exists("myDir") == false) {
FileSystem.createDirectory("myDir");
}
} );
}
};
The best thing about this approach (as opposed to embedding the code inside an SWF and parsing it later) is that, as you can see, the necessary changes are very small.
Another benefit is that it would be possible to put any haxe code in the frames (inside the Flash Professional tool), code that doesn’t have to be valid in actionscript too.
What do you thing? Could you implement something like this so that we can put haxe code on frames in Flash Professional?