Skip to content

Instantly share code, notes, and snippets.

@fireyang
Created April 9, 2013 13:27
Show Gist options
  • Save fireyang/5345683 to your computer and use it in GitHub Desktop.
Save fireyang/5345683 to your computer and use it in GitHub Desktop.
enter frame callback without events
package {
import flash.display.Loader;
import flash.display.MovieClip;
import flash.events.Event;
import flash.system.LoaderContext;
import flash.utils.ByteArray;
public class Framesync {
// Raw SWF bytecode (contains two empty keyframes on the main timeline)
static private const bytecode:Vector.<int> = new <int>[
0x46, 0x57, 0x53, 0x11, 0x36, 0x00, 0x00, 0x00,
0x30, 0x0A, 0x00, 0xA0, 0x00, 0x01, 0x02, 0x00,
0x44, 0x11, 0x08, 0x00, 0x00, 0x00, 0x43, 0x02,
0xFF, 0xFF, 0xFF, 0xBF, 0x15, 0x05, 0x00, 0x00,
0x00, 0x01, 0x00, 0x30, 0x00, 0x00, 0x7F, 0x10,
0x04, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00,
0x40, 0x00, 0x40, 0x00, 0x00, 0x00
];
// Holds the parsed SWF file
private var loader:Loader = new Loader();
// Function to call when the runtime enters a new frame
private var callback:Function = null;
// Constructor
public function Framesync( callback:Function ) {
var i:int = 0;
var n:int = bytecode.length;
var b:ByteArray = new ByteArray();
var c:LoaderContext = new LoaderContext();
while( i < n ) {
b[i] = bytecode[i];
i++;
}
c.allowCodeImport = true;
loader.loadBytes( b, c );
loader.contentLoaderInfo.addEventListener( Event.INIT, initialize );
private::callback = callback;
}
// Called when the SWF bytecode has been parsed
private function initialize( e:Event ):void {
MovieClip(loader.content).addFrameScript( 0, callback, 1, callback );
}
}// EOC
}
@gopig
Copy link

gopig commented Apr 10, 2013

这也是靠加载进来的swf的enterframe机制的啊...看不出有什么区别呢..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment