Listener in main document class, for custom dispatch event, from another class does not respond or call function
Hi a problem due to lack of knowledge:
I have a document class called Main.as In the class constructor I have the following listener:
enter code here
var listeningFORModeChangeToStudent:Sprite = new Sprite;
listeningFORModeChangeToStudent.addEventListener(TellAllModeChangeToStudent.STUDENT,exp);
addChild(listeningFORModeChangeToStudent);
function exp(event:Event){
trace("exp");
}
In a class called TellAllModeChangeToStudent I have a despatcher:
enter code here
public class TellAllModeChangeToStudent extends EventDispatcher{
public static const STUDENT:String = "student";
public function TellAllModeChangeToStudent() {
}
public function tellAllModeChangeToStudent(){
dispatchEvent(new Event(STUDENT));
trace("event despatched");
}
}
}
In a third class I make a call to the despatcher in the p开发者_开发百科revious class:
enter code here
var ThisTellAllModeChangeToStudent:TellAllModeChangeToStudent = new TellAllModeChangeToStudent;
ThisTellAllModeChangeToStudent.tellAllModeChangeToStudent();
I have trace statements in eveything and from this I know the despatcher in TellAllModeChangeToStudent is being called.
The problem is that the listener in the main.as is not calling the function exp. I cant see why and I dont know how to check if the listener is actually seeing the dispatch event?
This is my first attempt at using as3 depatcher and listeners. Help and guidence sought. Kindest regards Adrian.
First, this line in your Main.as class,
var listeningFORModeChangeToStudent:Sprite = new Sprite;
should say,
var listeningFORModeChangeToStudent:TellAllModeChangeToStudent = new TellAllModeChangeToStudent();
Second, the TellAllModeChangeToStudent class should extend Sprite instead of EventDispatcher.
public class TellAllModeChangeToStudent extends Sprite
Third, you are making 2 different TellAllModeChangeToStudent instances one in the Main class and one in the third class. You are listening for the event on one of them and dispatching the event from the other. You need to send a reference of the TellAllModeChangeToStudent instance in Main.as to the third class and dispatch the event from that instance.
精彩评论