How do I make my own events for my custom classes in Actionsctipt 3.0? Need to check when boolean prop changes.

Posted on 2007-08-05
Last Modified: 2013-11-05

I have a user class that loads the user's data via XML. I have an event when the XML completes loading to grab the values and assign the class's properties to those values. When it is done I have a boolean property ([B]xmlLoaded[/B]) for the class that is set to true when the other properties are done being filled by xml. When I make an object out of this class, I want to be able to check when the [B]xmlLoaded [/B]boolean changes from where I instantiated the object. Is this possible? What alternatives are there? I don't really want to use onEnterFrame, it feels like cheating. Can't I just make my own event?

here are the 2 classes:

package {
    import flash.display.*;
      import UserPack.*;
      import flash.utils.*;
    public class DocControl extends MovieClip {
        var self_mc:Self;
        public function DocControl() {
      this.self_mc = new Self();
      // need to check when "self_mc.xmlLoaded" changes to true to make sure I have the required data for the next function.

note: User extends from a MovieClip. It has basic user data in it.
package UserPack {
      public class Self extends UserPack.User {
            import flash.utils.*;
            public var emailAddress:String;
            public var xmlLoaded:Boolean;
            function Self() {
                  this.xmlLoaded = false;
                  classVar = this;
            public function loadXMLData():void {
                  var loader:URLLoader = new URLLoader();
                  loader.dataFormat = "text";
                  loader.addEventListener(Event.COMPLETE, this.setProperties);
                  loader.load(new URLRequest("TestUser.xml"));

            private function setProperties(loaderEvent:Event):void {
                  try {
                        var Database:XML = new XML(;
                        Database.ignoreWhitespace = true;
                        Database.ignoreComments = true;
                        var dataObj:Object = new Object();
                        for each(var userData:XML in Database.users[0].elements()) {
                              this[] = userData;
                        this.xmlLoaded = true;
                  } catch(xmlError:TypeError) {
Question by:Ryratt
    LVL 34

    Accepted Solution

    In AS3, you can create your own Custom Event and and invoke it using "dispatchEvent()" Method.
    here is more details:
    (Courtesy:  ActionScript 3.0 Language and Components Reference)

    The following example uses the classes EventDispatcherExample and CustomDispatcher, a subclass of EventDispatcher, to show how a custom event is created and dispatched. The example carries out the following tasks:
    The constructor of EventDispatcherExample creates a local variable dispatcher and assigns it to a new CustomDispatcher instance.
    Inside CustomDispatcher, a string is set so that the event has the name action, and the doAction() method is declared. When called, this method creates the action event and dispatches it using EventDispatcher.dispatchEvent().
    The dispatcher property is then used to add the action event listener and associated subscriber method actionHandler(), which simply prints information about the event when it is dispatched.
    The doAction() method is invoked, dispatching the action event.
    package {
        import flash.display.Sprite;

        public class EventDispatcherExample extends Sprite {

            public function EventDispatcherExample() {
                var dispatcher:CustomDispatcher = new CustomDispatcher();
                dispatcher.addEventListener(CustomDispatcher.ACTION, actionHandler);

            private function actionHandler(event:Event):void {
                trace("actionHandler: " + event);


    class CustomDispatcher extends EventDispatcher {
        public static var ACTION:String = "action";

        public function doAction():void {
            dispatchEvent(new Event(CustomDispatcher.ACTION));
    LVL 1

    Author Comment

    Ah ha! I understand now, thanks. I should dump my variable check and put something like the doAction() they have in your example. A WINNER IS YOU, aneeshchopra!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
    I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
    The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
    The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now