View Full Version : Problem with Event Handlers
Hi all. This is undoubtedly an embarrassingly stupid problem but I can't get my (very new-to-Flash) head around this. I've got a document class (MainClass) which creates a new object from the MinorClass. I would like the new MinorClass object to tell the MainClass when it's been created and done some stuff. I've added events as below but trace statements show that the order of operation is:
MainClass constructor,
MinorClass constructor
Event sent
Listener added
So the listener never gets to hear anything. How can I get around this?
public class MainClass extends Sprite {
private var minorClass:MinorClass;
public function MainClass()
{
var minorClass = new MinorClass();
minorClass.addEventListener("onHello", traceHello)
}
public function traceHello(evt:Event):void
{
trace("Hello from minorClass");
}
}
public class MinorClass extends Sprite {
public function MinorClass()
{
dispatchEvent(new Event("onHello"));
}
}
Eralmidia
11-02-2010, 02:05 PM
When coding, remember that flash read the code kinda like you read a book. It reads from the top, and works it way down. Now, any code in the constructor is called as soon as the class is created, or instantiated if you will.
You document class is connected to the main timeline, and will, as you probably know, run as soon as you movie starts. So, lets take a look at just what you do inside you constructor:
public function MainClass()
{
var minorClass = new MinorClass();
minorClass.addEventListener("onHello", traceHello)
}
First you create the new variable which holds the reference to the MinorClass, and you say
new MinorClass()
This syntax is actually a call to the constructor of you MinorClass class. This in turn, means that all code inside the constructor of MinorClass, which includes the event dispatch, is now execuded.
After this, flash will continue to the next line of the doc class constructor, which in turn will add a eventlistener to the event that allready has been disptached.
Thanks. So any ideas of how you can add an event listener to allow the calling class (MainClass) to be alerted when the called class (MinorClass) finishes its work? I'm sure this must be a pretty common requirement but I can't find a technique for doing it.
In partial answer to my own question, I came up with this. I'm not sure it's the best way but it works:
public class MainClass extends Sprite {
private var minorClass:MinorClass;
public function MainClass()
{
var minorClass = new MinorClass();
minorClass.addEventListener("onHello", traceHello)
minorClass.someFunction();
}
public function traceHello(evt:Event):void
{
trace("Hello from the event listener");
}
}
And
public class MinorClass extends Sprite
{
public function MinorClass()
{
}
public function someFunction():void
{
dispatchEvent(new Event("onHello"));
}
}
Eralmidia
11-03-2010, 04:16 AM
The thing is, that when flash reads
var minorClass:MinorClass = new MinorClass();
It will execute the code inside MinorClass() (the constructor of MinorClass). After that it will return to MainClass and continue the execution there. So you actually automatically know when its done with the MinorClass constructior, it's when it reaches the next line in the MainClass constructor.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.