PDA

View Full Version : Document Class access _root ?


HW2002
08-06-2007, 01:07 PM
Hello there,

I just started coding ActionScript 3 with Flash CS3, I'm trying to add a event to the stage from the document class, but it never occurs (dispatch)?

package {

import flash.events.MouseEvent;
import flash.display.MovieClip;

public class blah extends MovieClip {
public function blah()
addEventListener(MouseEvent.MOUSE_DOWN, ok);
}

public function ok(e:MouseEvent):void {
trace("test");
}
}
}

I don't get test when I click on my mouse, the stage is empty.

thanks for any help.

regards,
Hw2002

dr_zeus
08-06-2007, 05:26 PM
Have you tried this?

this.stage.addEventListener(MouseEvent.MOUSE_DOWN, ok);

I've found that specifically referencing the stage can have different results somtimes.

HW2002
08-06-2007, 05:39 PM
Have you tried this?

this.stage.addEventListener(MouseEvent.MOUSE_DOWN, ok);

I've found that specifically referencing the stage can have different results somtimes.

Thanks for your help, its working.

But what do you mean by "It can have different results sometimes?", since I developing something to a customer, and I want it to work always as I excepted it to work?

I hope you can tell me what you mean and thanks again for your help :)

dr_zeus
08-06-2007, 08:17 PM
The line of code I suggested will always work the way you expect. You misunderstood my meaning. What I ultimately meant is that your document class is not the stage. The reason your code wasn't working before is because the document class didn't contain anything that was clickable. To make it clickable, you need to draw something on it. I suggested using the stage because you can always listen for mouse events from the stage object regardless of what's been drawn.

HW2002
08-07-2007, 07:54 AM
I actually thought the document class was the root for my whole stage, but as I understand from you, its not :) and I have to put events on the stage and not on my document class.

this made me thing about the _root, I used to do something like this with AS2:

_root.onEnterFrame = function():void {
}

and not stage.onEnterFrame?

Do you have the time to explain and many thanks to you :D

senocular
08-07-2007, 11:19 AM
see "How stage, root, and MainTimeline Fit Together":
http://www.kirupa.com/forum/showthread.php?p=2129548#post2129548