How to access the stage from an AS3 class in Adobe

2019-04-08 10:52发布

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);

4条回答
Emotional °昔
2楼-- · 2019-04-08 11:16

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):

public function Main():void {
    this.addEventListener(Event.ADDED_TO_STAGE, init);
    super();    
}
...
private function init(e:Event):void {
...

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.

查看更多
等我变得足够好
3楼-- · 2019-04-08 11:19

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.

public function MyClass(){
    this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}

private function addedToStageHandler(e:Event):void{
    this.stage.addEventListener(KeyboardEvent.KEY_DOWN, dostuff);
}
查看更多
【Aperson】
4楼-- · 2019-04-08 11:28

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.

package  {
    import flash.events.KeyboardEvent;

    public class Eventhndl{

        private var obj:Object; //create local variable to refarance stage


        public function Eventhndl(objStage:Object):void{
            obj = objStage; //make local refarance for stage inside the class

            obj.addEventListener(KeyboardEvent.KEY_DOWN,runit); //add the event listener
        }

        private function runit(Event:KeyboardEvent):void{
            trace("keyDownHandler: " + Event.keyCode);
            trace("ctrlKey: " + Event.ctrlKey);
            trace("keyLocation: " + Event.keyLocation);
            trace("shiftKey: " + Event.shiftKey);
            trace("altKey: " + Event.altKey);
        }
    }

}

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.

import Eventhndl;

var EH:Eventhndl = new Eventhndl(stage); 
查看更多
狗以群分
5楼-- · 2019-04-08 11:38

"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:

 import flash.display.*;
 import flash.events.*;
 import flash.ui.*; 

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.

查看更多
登录 后发表回答