Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
Medium Priority
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.net.*;
      import flash.events.*;
      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.net.*;
            import flash.events.*;
            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(loaderEvent.target.data);
                        Database.ignoreWhitespace = true;
                        Database.ignoreComments = true;
                        var dataObj:Object = new Object();
                        for each(var userData:XML in Database.users[0].elements()) {
                              this[userData.name()] = userData;
                        this.xmlLoaded = true;
                  } catch(xmlError:TypeError) {
Question by:Ryratt
LVL 34

Accepted Solution

Aneesh Chopra earned 2000 total points
ID: 19636349
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;
    import flash.events.Event;

    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);

import flash.events.EventDispatcher;
import flash.events.Event;

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

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

Author Comment

ID: 19640554
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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
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.
Suggested Courses
Course of the Month13 days, 1 hour left to enroll

580 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