-
Senior Member
Class syntax for setting up an event listener
I am trying to create a class (myClass) that listens to events of another class instance (myProdEventDispatcher).
this tutorial seems to work great but I have difficulties shifting the listener code to class syntax.
Here's my code.
Code:
Class myClass{
…
…
//event handling is optional for this class
//this method is called from the constructor only in cases when the class instance
//needs to grab events.
private function setProdListeners(){
var listenerObject = new Object;
//myProdEventDispatcher class sends onProdKey event
//I add an event listener to grab it's events
_global.myProdEventDispatcher.addEventListener("onProdKey", listenerObject);
//invoke each time the event is trigered
//Flash never get's here, I can see onProdKey is sent from myProdEventDispatcher
//but can not get the trace
listenerObject.onProdKey = function(evtObj){
trace("onProdKey was trigered")
}
}
}
any ideas?
Thanks
LeoBeer
-
half as fun, double the price
There's really nothing wrong with that implemenetation aside from the fact you lose your listener reference (thereby making it rather difficult to remove it if you need to).
Maybe you're not dispatching the event correctly?
-
Senior Member
Thanks senocular,
the event is being dispatched, I am able to grab it from a different listener on the _root.
I would want to remove the listener later on so I need a reference to it, I guess I should declare it on top of my class, is this correct?
-
Re Member
Suppose you have class with names:
class myProdEventDispatcher{}
class myClass{}
in class myProdEventDispacher.as
Code:
class myProdEventDispatcher{
...
function someFunction(){
// you need to establish myClass instance to
// reference myClass
var refObj:myClass=new myClass(this);
// set dispatch target to myClass instance
var eventObject:Object = {target:refObj, type:'onProdKey'};
dispatchEvent(eventObject);
}
}
in class myClass.as
Code:
class myClass{
private var inRefObj:Object;
private var myListnerObj:Object = new Object;
public function myClass(obj){
trace("initialized myClass");
inRefObj=obj;
myListnerObj.onProdKey = function(evtObj) {
trace("onProdKey was trigered");
};
// add event listerner to class instance
inRefObj.addEventListener("onProdKey",myListnerObj);
}
}
The key point is that you need a communication channel between 2 unrelated classes.
If you set:
Code:
var eventObject:Object = {target:this, ...
you are referencing its own instance on _root
If you set:
Code:
var refObj:myClass=new myClass(this);
var eventObject:Object = {target:refObj, ...
you are pointing event receiver to myClass to trigger it's eventhandler.
Wish it solves your problem
Last edited by websam; 03-21-2005 at 10:40 AM.
-
Senior Member
Thanks websam,
The code looks fine, there's only one problem which prevents it from serving me purpose:
In your example myProdEventDispacher already knows about myClass, this might not be possible in my case.
In my case I know for sure myProdEventDispacher will be present.
I don't know if:
1. myClass will be present and in how many instances.
2. All myClass instances would like to be subscribed to events.
3. Subclasses of myClass would like to listen to myProdEventDispacher events.
Can you see my point?
-
half as fun, double the price
I think this sticks to your original class's idea.
PHP Code:
// myClass.as
class myClass {
private var listenerObject:Object;
function myClass(grabEvents:Boolean){
if (grabEvents == true){
listenerObject = new Object();
setProdListeners();
}
}
private function setProdListeners(){
_global.myProdEventDispatcher.addEventListener("onProdKey", listenerObject);
listenerObject.onProdKey = function(evtObj){
trace("onProdKey was trigered");
}
}
}
PHP Code:
// myProdEventDispatcher.as (basic implementation)
class myProdEventDispatcher {
private static var dispatcherSetup = mx.events.EventDispatcher.initialize(myProdEventDispatcher);
public static var addEventListener:Function;
public static var removeEventListener:Function;
public static var dispatchEvent:Function;
}
PHP Code:
// main movie
var myInstance:myClass = new myClass(true);
myProdEventDispatcher.dispatchEvent({type:"onProdKey"});
That should trace the statement.
-
Senior Member
thanks senocular, that static functions trick solved my problem.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|