TwelvestoneFlash

Custom events on classes


Sign in

  • Waiting for Godot ( 720 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 4.8 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.8 k posts )
    general front end design an...
  • Back End ( 9.6 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
blacksanta_69
 
2010-02-08

I'm working on a simple class that loads configuration information from a file. I would like to load the file and create an event in the document class to proceed with parsing the information once the data is loaded. I've done this in the past by creating a function in the child class and "overriding" the functionality by creating a reference to a new method in the parent class. For example:

//main.as - document class package{ import flash.display.Sprite;

import loadingClass;

public class main extends Sprite{
    var loadFile:loadingClass;

    public function main():void{
        loadFile = new loadingClass("somefile.json");
        loadFile.onComplete = init;
    }

    public function init():void {
        //the file is ready
    }
}

}

//loadingClass.as package {

import flash.net.*;

import flash.events.*;

public class loadingClass {

    private var fileLoader:URLLoader;

    //Define a function to be overwritten by the parent
    public var onComplete:Function;

    public function loadingClass(path:String):void{
        if (path == null){return;}

        var filePathRequest:URLRequest = new URLRequest(path);
        fileLoader = new URLLoader (filePathRequest);

        setListeners(fileLoader);

    }
    private function setListeners(dispatcher:IEventDispatcher):void{
        dispatcher.addEventListener(Event.COMPLETE, createObj);
    }

    public function createObj(evt:Event):void{
        //Call the onComplete function which should have been
        //overwritten with a parent function
        onComplete();
    }
}

}

I suspect this isn't the "proper" way to do this - is there a generally preferred method, like using an IEventDispatcher? Ideally, I'd like the implementation to be something like:

//main.as loadFile = new loadingClass("somefile.txt"); loadFile.addEventListener("onComplete", init);

lithium
 
2010-02-09

you can extend EventDispatcher, which will give you all the event handling stuff for free, or you can instantiate an instance of eventdispatcher in a class and route your events through that. eg:

/** By extending EventDispatcher **/

package {

import flash.events.*;

public class Test extends EventDispatcher
{
    public function Test()
    {
        super(this);

        // Now you're good to add, remove and dispatch events with this class
    }
}   

}

/** Without extending EventDispatcher **/

package {

import flash.events.*;

public class Test
{
    protected var dispatcher:EventDispatcher;

    public function Test()
    {
        super();

        dispatcher = new EventDispatcher ( this );
        // Now you have to route the methods through your dispatcher instance.

    }

    public function addEventListener ( type : String, method : Function /* all the other ones here */ ) : void
    {
        dispatcher.addEventListener ( type, method );
    }

    public function removeEventListener ( type : String, method : Function ) : void
    {
        dispatcher.addEventListener ( type, method );
    }

    public function dispatchEvent ( evt:Event ):void
    {
        dispatcher.dispatchEvent ( evt );
    }
}   

}

helenk579
 
2010-07-01

Thanks you for the post.

persist
 
2010-07-01

Welcome to the board.

:beer:

JLM
 
2010-07-04

Suggest you look at Signals as an alternative.

Sorry, you must be a member to post to a conversation. Either log in or sign up to get involved.
TwelvestoneFlash

Custom events on classes