如何从 Adob​​e Flash 中的 AS3 类访问舞台

发布于 2024-08-29 22:46:04 字数 249 浏览 2 评论 0原文

我遇到的问题是我正在使用键盘事件监听器来使影片剪辑运行。由于我是一名大学生,我正在为作业创建这个,但我们被迫使用 as3 类。

当我在主时间线中运行代码时,没有问题。但是,当我尝试从另一个类访问它时(在相关影片剪辑上使用“导出为 ActionScript”),我收到错误,他无法解决舞台问题。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

此生挚爱伱 2024-09-05 22:46:04

AS3 中的类在您实际将其放置在那里之前不会出现在舞台上。结果,“this.stage”在编译时将为空。您可以通过使用 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);
}

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);
}
甚是思念 2024-09-05 22:46:04

“1120:访问未定义的属性键盘。
这就是你的答案。您尚未定义键盘属性。这意味着您还没有导入到包中。

应该看起来像这样:

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

建议:
对进口有更深入的了解。
尝试使用 flash builder,它对初学者来说更好,并且自动导入类,因此您不需要记住所有内容。

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

背叛残局 2024-09-05 22:46:04

这个答案已经帮助了我一百万次,但我还没有足够的积分来弹出它,或者我会的。

当您在将舞台上的任何内容添加到舞台之前尝试访问舞台上的任何内容时,通常会发生这种情况。有一段时间,我在所有项目主类的构造函数中使用 init() ,但由于这个问题,我不再这样做。现在,我用这个替换了它(其中 Main 是类构造函数/名称):

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

我希望这对阅读我在 flash 上提供 init() 想法的任何书籍的其他人有所帮助。

还有……谢谢格雷格·W.

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.

凉城凉梦凉人心 2024-09-05 22:46:04

当您创建类时,您必须从类内部引用阶段,因为它无法全局访问,您必须将其传递到类中,这里是在类中使用阶段事件侦听器的示例。

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

}

将文件另存为 Eventhndl.as,现在您只需添加此类的实例并传递侦听其事件所需的任何对象,以下是操作方法。

import Eventhndl;

var EH:Eventhndl = new Eventhndl(stage); 

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); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文