View Full Version : Scope, classes, subclasses and eventdispatcher question
eviljelly
11-11-2005, 02:03 PM
Hi,
I am trying to create a class that sends out an event on a function being called, which is then received by a listener in all instances of it's subclass.
class mainclass{
// Properties
public function dispatchEvent() {};
public function addEventListener() {};
public function removeEventListener() {};
// Constructor
public function mainclass (){
mx.events.EventDispatcher.initialize(this);
}
// Methods
public function ping(){
dispatchEvent({target:this,type:"ping"});
}
}
class subclass extends mainclass{
public function subclass(){
this.addEventListener("ping", this);
}
public function ping(){
trace("ping");
}
}
Timeline:
mainclass1 = new mainclass();
subclass1 = new subclass();
mainclass1.ping();
I guess it's a scope issue for:
this.addEventListener("ping", this);
But after trying 'super', '_parent', 'mainclass' and 'mainclass1' without success I began to wonder if it is even possible?
Any help seeing the error of my ways would be fantastic, thanks.
try :
subclass1.ping();
it will work (i know that you not behinf this)
public function mainclass (){
trace("you willsee this is executed 2 time !")
mx.events.EventDispatcher.initialize(this);
}
basicly the EventDispatcher from mainclass1
and the one from subclass1 are 2 diferent things !
class mainclass {
static var INIT = 0;
// Properties
static function dispatchEvent() {
}
static function addEventListener() {
}
static function removeEventListener() {
}
// Constructor
public function mainclass() {
if (!INIT) {
trace("INIT");
INIT = 1;
mx.events.EventDispatcher.initialize(mainclass);
}
}
// Methods
function pingit() {
trace("send");
mainclass.dispatchEvent({target:this, type:"ping"});
}
}
//////////////////
//////////////////
class subclass extends mainclass {
public function subclass() {
trace("Listen");
mainclass.addEventListener("ping", this);
}
function ping() {
trace("ping");
}
}
//////////////////
/////////////////
mainclass1 = new mainclass();
subclass1 = new subclass();
a= new subclass();
subclass1.pingit();
mainclass1.pingit();
a.pingit();
hangalot
11-11-2005, 03:00 PM
the event dispatcher methods must not be static.
class mainclass {
static var INIT = 0;
// Properties
private function dispatchEvent() {
}
public function addEventListener() {
}
public function removeEventListener() {
}
// Constructor
public function mainclass() {
mx.events.EventDispatcher.initialize(mainclass);
}
// Methods
function pingit() {
trace("send");
dispatchEvent({target:this, type:"ping"});
}
}
that is just a bad idea. think of it like this. i create multiple instances of this class but the event dispatcher is on class level, not on instance level so one click event will notify ALL instances
hangalot
11-11-2005, 03:01 PM
also extending that class will automatically give you the eventdispatcher implementation
origin target :
which is then received by a listener in all instances of it's subclass.
?????
that is just a bad idea. think of it like this. i create multiple instances of this class but the event dispatcher is on class level, not on instance level so one click event will notify ALL instances
public function mainclass() {
//whit out my methode to prevent multiple execution here
//you will lose already listening objects on a reinitialization !!!
mx.events.EventDispatcher.initialize(mainclass);
}
eg:
psoido code :
mx.events.EventDispatcher.initialize(mainclass);
mainclass.addEventListener("ping", Bla);
mx.events.EventDispatcher.initialize(mainclass);
mainclass.addEventListener("ping", something);
//Bla will no longer listen !!!
hangalot
11-11-2005, 05:00 PM
look in the debugger how its inmplemented. especially the __q object
now i am totaly :confused:
what do you mean by "__q" ????
and if event dispatcher isn't on class level how shoud ALL instances listen to it ?
eviljelly
11-11-2005, 05:18 PM
Thanks for your quick replies. Xeef's method does work, which is great and i'm very appreciative for this :)
Concerning the double call of the mainclass constructor - is it that the constructor function for mainclass is called additionally when an instance of a subclass that extends mainclass is created?
Suppose I wanted to set a variable in the subclass instance when the event is detected - the way I currently have it sets the container variable In mainclass instead of subclass, another scope issue no doubt:
class subclass extends mainclass {
public var container = 0;
public function subclass() {
trace("Listen");
mainclass.addEventListener("ping", this);
}
function ping() {
container += 1;
trace(container);
}
}
Basically when a setter method is called in the single instance of mainclass, I want every subclass instance to change an internal property. I think because it's only a single instance of mainclass, that it allows for this use of static.
I'd be interested to hear from hangalot as to how to achieve the same results, possibly using the mainclass instance rather than the static mainclass to dispatch and listen to the event.
Thanks for the great help, you've dug me out of a dark hole. :)
dynamic class mainclass {
static var INIT = 0;
// Properties
static function dispatchEvent() {
}
static function addEventListener() {
}
static function removeEventListener() {
}
// Constructor
public function mainclass() {
if (!INIT) {
trace("INIT");
INIT = 1;
mx.events.EventDispatcher.initialize(mainclass);
}
}
// Methods
function pingit() {
trace("send");
mainclass.dispatchEvent({target:this, type:"ping"});
}
}
/////////////////////////////
class subclass extends mainclass {
var MyID;
var WhatEver;
public function subclass() {
MyID = random(1000);
WhatEver = 0;
mainclass.addEventListener("ping", mx.utils.Delegate.create(this, this.ping));
}
function ping() {
trace("ping "+MyID+" "+WhatEver++);
}
}
/////////////////////
mainclass1 = new mainclass();
subclass1 = new subclass();
a= new subclass();
subclass1.pingit();
mainclass1.pingit();
a.pingit();
eviljelly
11-11-2005, 06:30 PM
Delicious.
Thanks ever so much, it's working as it should now :)
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.