如何从 Adobe Flash 中的 AS3 类访问舞台
我遇到的问题是我正在使用键盘事件监听器来使影片剪辑运行。由于我是一名大学生,我正在为作业创建这个,但我们被迫使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AS3 中的类在您实际将其放置在那里之前不会出现在舞台上。结果,“this.stage”在编译时将为空。您可以通过使用 ADDED_TO_STAGE 事件延迟绑定侦听器直到时间合适来解决此问题。
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.
“1120:访问未定义的属性键盘。
这就是你的答案。您尚未定义键盘属性。这意味着您还没有导入到包中。
应该看起来像这样:
建议:
对进口有更深入的了解。
尝试使用 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:
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.
这个答案已经帮助了我一百万次,但我还没有足够的积分来弹出它,或者我会的。
当您在将舞台上的任何内容添加到舞台之前尝试访问舞台上的任何内容时,通常会发生这种情况。有一段时间,我在所有项目主类的构造函数中使用 init() ,但由于这个问题,我不再这样做。现在,我用这个替换了它(其中 Main 是类构造函数/名称):
我希望这对阅读我在 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):
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.
当您创建类时,您必须从类内部引用阶段,因为它无法全局访问,您必须将其传递到类中,这里是在类中使用阶段事件侦听器的示例。
将文件另存为 Eventhndl.as,现在您只需添加此类的实例并传递侦听其事件所需的任何对象,以下是操作方法。
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.