简体   繁体   English

如何从Adobe Flash中的AS3类访问舞台

[英]How to access the stage from an AS3 class in Adobe Flash

The problem I've encountered is that I am using a keyboardEventListener to make a movieclip run around. 我遇到的问题是我使用keyboardEventListener来运行一个movieclip。 As I'm a college student, I'm creating this for an assignment but we are forced to use as3 classes. 由于我是一名大学生,我正在为一项任务创建这个,但我们被迫使用as3课程。

When I run the code in the maintimeline, there is no problem. 当我在maintimeline中运行代码时,没有问题。 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. 但是当我尝试从另一个类访问它时(在动画片段上有一个'Export for ActionScript')我得到一个错误,他无法解决这个阶段。

this.stage .addEventListener(KeyboardEvent.KEY_DOWN, dostuff); this.stage .addEventListener (KeyboardEvent.KEY_DOWN,dostuff);

A class in AS3 is not on the stage until you actually place it there. 在你真正把它放在那里之前,AS3中的一个类不在舞台上。 As a result, "this.stage" will be null at compile time. 因此,“this.stage”在编译时将为null。 You can get around this problem by using the ADDED_TO_STAGE event to delay binding your listeners until the time is right. 您可以通过使用ADDED_TO_STAGE事件来延迟绑定侦听器,直到时机成熟,从而解决此问题。

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

private function addedToStageHandler(e:Event):void{
    this.stage.addEventListener(KeyboardEvent.KEY_DOWN, dostuff);
}

"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. “1120:访问未定义的属性键盘。有你的答案。你还没有定义键盘属性。这意味着你还没有导入到包中。

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. 尝试使用Flash Builder,它对初学者和自动导入类更好,所以你不需要记住一切。

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. 将文件保存为Eventhndl.as,现在您只需添加此类的实例并传递您需要监听其事件的任何对象,以下是如何执行此操作。

import Eventhndl;

var EH:Eventhndl = new Eventhndl(stage); 

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. 我有一段时间在我所有项目主类的构造函数中使用了init(),但由于这个问题,我不再这样做了。 Now, instead I have replaced it with this (where Main is the class constructor/name): 现在,我已将其替换为this(其中Main是类构造函数/名称):

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. 我希望这可以帮助其他读过我在flash上​​做过的任何书籍的人提供init()的想法。

And..thanks Greg W. 并且......谢谢Greg W.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM