I haven’t touched Flash in a while but I have a site to do so my “skills” need some brushing up on. A while ago, I created a class that registers classes and dispatches events to them and they would react accordingly. It was my alternative to the AS2 Broadcaster class. I’ve seen some other alternatives out there but mine works fine. However, I was wondering if there was an overall better alternative to this dispatching method or if my class can be cleaned up at all?
Also, can someone please direct me to or suggest a good environment for creating typical OOP sites and classes. i.e -->
MainCode.as
loading/
- Loader.as
shell/ - Display.as
model/ - Model.as
Model Class
package model {
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class Model extends EventDispatcher {
static var xml:XML;
static var xmlLoader:URLLoader;
function Model(){}
// event dispatch methods
static var dispatcher:EventDispatcher;
static var aListeners:Array;
public static function setDispatcher():void
{
dispatcher = new EventDispatcher();
aListeners = new Array();
}
public static function addListener(o:Object):void
{
aListeners.push(o);
}
public static function sendUpdate(s:String):void
{
for(var i:uint = 0; i<aListeners.length; i++)
{
aListeners*.dispatchEvent(new Event(s));
}
}
// xml methods
public static function loadXML():void
{
xmlLoader = new URLLoader(new URLRequest("xml/siteData.xml"));
xmlLoader.addEventListener(Event.COMPLETE, xmlComplete);
}
static function xmlProgress(e:ProgressEvent):void
{
//trace("xml loading");
}
static function xmlComplete(e:Event):void
{
xml = new XML(e.target.data);
xml.ignoreWhitespace = true;
sendUpdate("xmlDone");
}
public static function XMLNodeByName(s:String, attribute:String):Array
{
var arr:Array = new Array();
for(var i:uint = 0; i<xml.child(s).length(); i++)
{
arr.push(xml.child(i)["@" + attribute]);
}
return arr;
}
public static function XMLNodeByNum(num:Number):Array
{
var arr:Array = new Array();
for(var i:uint = 0; i<xml.child(num).children().length(); i++)
{
arr.push({name:xml.child(num).child(i).@name, data:xml.child(num).child(i)});
}
return arr;
}
}
}
example of usage:
var oShell:Shell;
...
var mShell:MovieClip = new MovieClip();
mClip.addChild(mShell);
oShell = new Shell(mShell);
Model.addListener(oShell);
in Shell class there would be:
addEventListener("update", eventHandler);
addEventListener("xmlDone", eventHandler);