PDA

View Full Version : Custom Event Dispatcher.



Rizander
August 25th, 2009, 02:28 PM
Afternoon,

So I have a small game where I have implemented a* path finding. I have a spot where a player moves around the screen along the path found. I have a section where i know the user is at the end of the path and I am looking to dispatch an event at this moment so I can do things at the end of the path.

i have imported the aStar_path_dispatcher file into the move class then i do,

aStarDispatch.dispatchFinishMove();
at the spot where i know the path movement is finished.


my aStar_path_dispatcher class looks like this:


package com.aStar {

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

public class aStar_path_dispatcher extends EventDispatcher {

public static var ON_FINISH_MOVE:String = "Move Finished";

public function aStar_path_dispatcher(){

}//end constructor

public function dispatchFinishMove():void{
dispatchEvent(new Event(aStar_path_dispatcher.ON_FINISH_MOVE));
trace("event fired");
}//end dispatchFinishMove
}//end class
}//end package

and it does trace "event fired".

then in my main document class i import the aStar_path_dispatcher again, and set up an instance of it: then add a listener:

aStarDispatch.addEventListener(aStar_path_dispatch er.ON_FINISH_MOVE, aStarMoveFinished);

...

private function aStarMoveFinished(e:Event){
trace("Move finished");
}//end move Finished



however this "move finished" doesn't seem to want to work. I have never used custom events before, so I must be doing something stupidly wrong. Any help is appreciated thanks.

Rizander
August 25th, 2009, 02:45 PM
I seem to be able to make it work if I put the listener in the same class as the class i dispatch from. Is this a limitation or am I doing something wrong? I could do the same thing with if else statements.... I must be missing something.

thatsasif
August 25th, 2009, 03:01 PM
http://www.kirupa.com/forum/showpost.php?p=1899603&postcount=79

Check this link.