Skip to content

Instantly share code, notes, and snippets.

@alecmce
Created November 26, 2009 22:19
Show Gist options
  • Save alecmce/243692 to your computer and use it in GitHub Desktop.
Save alecmce/243692 to your computer and use it in GitHub Desktop.
A strategy for creating an application 'enter-frame' signal in AS3
package
{
import org.osflash.signals.Signal;
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.getDefinitionByName;
/**
* The EnterFrameDispatcher is injected into the Main class through the [Frame] MetaData tag.
*
* The class creates a frame signal and generates two frames which cycle to dispatch the frame
* signal on every frame. It then injects this object into the Main class and calls the Main
* class's init() method.
*
* @see http://alecmce.com/as3/replacing-enter_frame
*
* @author Alec McEachran
*/
public class EnterFrameDispatcher extends MovieClip
{
public var frame:Signal;
public function EnterFrameDispatcher()
{
frame = new Signal();
addFrameScript(0, frameMethod, 1, frameMethod);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
nextFrame();
}
private function onEnterFrame(event:Event):void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
init();
play();
}
private function frameMethod():void
{
frame.dispatch();
}
private function init():void
{
var mainClass:Class = Class(getDefinitionByName("Main"));
if (mainClass)
{
var app:* = new mainClass();
app.frame = frame;
addChild(app);
app.init();
}
}
}
}
package
{
import org.osflash.signals.Signal;
import flash.display.Shape;
import flash.display.Sprite;
/**
* A Main application class that uses the [Frame] MetaData Tag to use the EventFrameDispatcher class
* to configure itself. This class contains a frame signal which is constructed and passed into it
* by the EventFrameDispatcher class before init() is called. Application functionality should be
* placed in the init() method
*
* @see http://alecmce.com/as3/replacing-enter_frame
*
* @author Alec McEachran
*/
[Frame(factoryClass="EnterFrameDispatcher")]
public class Main extends Sprite
{
/** the frame signal is an as3signal that is dispatched on every ENTER_FRAME */
public var frame:Signal;
/**
* Add functionality here to launch your application and do interesting things
*/
public function init():void
{
// functionality here...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment