The problem I've encountered is that I am using a keyboardEventListener to make a movieclip run around. As I'm a college student, I'm creating this for an assignment but we are forced to use as3 classes.
When I run the code in the maintimeline, there is no problem. But when I try to access it from another class (with an 'Export for ActionScript' on the movieclip in question) I get an error he can't address the stage.
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, dostuff);
this answer has helped me a million times, but i dont yet have enough points to pop it up one, or i would.
This happens generically when you try to access anything on the stage before it is added to stage. I was, for a while, using an init() in the constructor of all my projects main classes, but because of this issue, I no longer do. Now, instead I have replaced it with this (where Main is the class constructor/name):
I hope this helps anyone else who read any of the books I did on flash, that offer the init() idea.
And..thanks Greg W.
A class in AS3 is not on the stage until you actually place it there. As a result, "this.stage" will be null at compile time. You can get around this problem by using the ADDED_TO_STAGE event to delay binding your listeners until the time is right.
When you create class you have to refer the stage from inside of your class coz its not accessible globally you have to pass it into the class , and here is a example for use stage event listener inside a class.
save the file as Eventhndl.as and now you can just add the instance of this class and pass whatever the object that you need to listen its event, here is how to do that.
"1120: Access of undefined property Keyboard. There's your answer. You haven't defined the keyboard properties. Which means you haven't imported to the package.
should look something like this:
Advice: have a deeper look into importing. try using flash builder, its much better for beginners and auto import classes so u dont need to memorize everything.