Event Notifier
[ November 30, 2004 ] by Daisuke Yanagi
An event notification system for loading external movieclips and assets in a modular way.


A Large Project uses the style of loading swfs separeately. One of the benefits is that many coders only manage their own fla, and that reduces overhead for loading at the first time. How events, for example, like mouse clicks and key inputs, are notified from swfs to parent swf? It is a simple way to refer to a global variable that stores the data from events. But it is very important for OOP programming style to store variables and functions into an encapsulated object so that data is kept in an organized way.

First, notifierLoaded in loaded swf returns undefined when it starts loading, and then that loaded bytes get to total bytes invokes notifierLoaded() written in a loaded swf. The notifierLoaded function constructs a instance of Notifier Class and returns it to Loader Class. Next, Loader Class invokes addNotifier function with an argument as the instance returned and then Loader Class is listening to events from a notifier instance. When n.notify() is invoked in swf, Notifier Class generates a event object called "notify" and notify it to all objects is listening to.

class Loader implements NotifierListener{
  private var check:Number;
  private var container:MovieClip;

  function load(target:MovieClip, swf:String):Void{
    container = target.createEmptyMovieClip("___container", 1);
    container.loadMovie(swf);
    check = setInterval(function(owner:Loader):Void{
		
    var container = owner.container;
    var total:Number = container.getBytesTotal();
    var loaded:Number = container.getBytesLoaded();
    if(loaded > 4 && loaded == total){
        if(container.notifierLoaded!=undefined){
          clearInterval(owner.check);
          var n = container.notifierLoaded(owner);
          owner.addNotifier(n);
        }
      }
    }, 100, this);
  }
  function addNotifier(o:Notifier):Void{
    o.addEventListener("notify", this);
  }
}

class Notifier{
  private var dispatchEvent:Function;
  public var addEventListener:Function;
  public var removeEventListener:Function;
  function Notifier(){
    mx.events.EventDispatcher.initialize(this);
  }
  function notify():Void{
    dispatchEvent({type:"notify"});
  }
}

interface NotifierListener{
  function addNotifier(o:Notifier):Void;
}

a keyframe in a loaded swf:

var n:Notifier;
function notifierLoaded(o:Loader):Notifier{
  if(n==undefined){
    n = new Notifier();
  }
  return n;
}


 
 
Name: Daisuke Yanagi
Location: Tokyo, JP
Age: 28
Flash experience: about 5yrs
Job: Designer, Flash Developer
Website: Designer, Flash Developer
 
 
| Homepage | News | Games | Articles | Multiplayer Central | Reviews | Spotlight | Forums | Info | Links | Contact us | Advertise | Credits |

| www.smartfoxserver.com | www.gotoandplay.biz | www.openspace-engine.com |

gotoAndPlay() v 3.0.0 -- (c)2003-2008 gotoAndPlay() Team -- P.IVA 03121770048