views:

31

answers:

1

I wanted to remove an event listener from main class stage, but i get the error 1120: Access of undefined property stage. How do I actually access the stage?

custom class:

import main;
main.disableVcam();

main class:

public static function disableVcam():void {
            trace("disable");
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, movevC);
        }
A: 

Unless the object is on the display stage, the stage object will be undefined (or null). You have to addChild the object for the stage object to have a value.

Edit: Perhaps you can handle this in the event handler?

protected function clickHandler(e :Event) :void {
    if (e.target.stage) {
        e.target.stage.removeEventListener(...);
    }
}

Edit2: Static methods don't have a stage, so to solve your problem you can make your Main-class a singleton, and work like this:

public class Main {
    static private var instance :Main;

    static public function getInstance() :Main {
        if (Main.instance == undefined) {
            Main.instance = new Main();
        }

        return Main.instance;
    }

    // The rest of the class goes here
}


// snip

import Main;

public static function disableVcam():void {
    trace("disable");
    Main.getInstance().stage.removeEventListener(MouseEvent.MOUSE_MOVE, movevC);
}

If your Main-class is the main class of the project, you need to assign the static instance variable's value in the constructor.

nikc
i have the classes added to stage using the Event.ADDED_TO_STAGE, but the class is only called when a button from another dynamic class is push, and its like few layers of other classes. I can get only the trace statement but cant get the mouseevent working
Hwang
Perhaps you could try using `event.target.stage` in your event-handler.
nikc
nope. doesnt seems to work.
Hwang
Ah, sorry, my bad. I only now noticed the magic word, which in this case is `static`. Only an instance of a class (= an object) will have a `stage`, as static classes cannot be added to the display list.
nikc
Added an example of a possible solution.
nikc