无法访问空对象引用的属性或方法

发布于 2024-09-18 09:49:30 字数 966 浏览 8 评论 0原文

我尝试在 Flex 中为我用 Flash 编写的项目进行预加载。 我在这个网站的帮助下做了这个 链接文本 我的 Flash 项目在名为 Game 的主类中有下一个源

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);

private function keyDown(event:KeyboardEvent) {
   if (event.keyCode == 81 && q_was_push == false) q_was_push = true;
   if (event.keyCode == 81) press_q = true;
   if (event.keyCode == 65) press_a = true;
   if (event.keyCode == 83) press_s = true;
   if (event.keyCode == 32) press_space = true;
} ...

当我获取由 Flex 制作的新 swf 文件时,出现错误 TypeError:错误#1009:无法访问空对象引用的属性或方法。 在 Game() 中,

如果我评论

//stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
//stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);

Flex 应用程序工作,但 Flash 应用程序对按钮按下没有反应,

请问我如何使预加载器和工作按钮一起工作

I try to do preloder in Flex for my project written in Flash.
I make this with the help of this site
link text
My Flash project have next source in main class called Game

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);

private function keyDown(event:KeyboardEvent) {
   if (event.keyCode == 81 && q_was_push == false) q_was_push = true;
   if (event.keyCode == 81) press_q = true;
   if (event.keyCode == 65) press_a = true;
   if (event.keyCode == 83) press_s = true;
   if (event.keyCode == 32) press_space = true;
} ...

When I take new swf file maked by Flex, I have error
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Game()

if I comment

//stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
//stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);

Flex application work but Flash application does not react to button presses

Please how I can make preloader and work buttons together

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

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

发布评论

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

评论(2

嗼ふ静 2024-09-25 09:49:30

在将显示对象添加到显示列表之前,stage 属性将为 null。收听 addedToStage 事件并从那里添加主要侦听器。

addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
function onAddedToStage(e:Event):void
{
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
}

The stage property will be null until a display object is added to the display list. Listen to the addedToStage event and add the key listeners from there.

addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
function onAddedToStage(e:Event):void
{
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
}
她如夕阳 2024-09-25 09:49:30

每当您需要访问舞台时,请让类在构造函数中侦听/检查它,并让您的 init 函数作为处理程序。

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    /**
     * ...
     * @author Brian Hodge
     */
    public class SomeClass extends Sprite
    {

        public function SomeClass() 
        {
            if (stage) _init();
            else addEventListener(Event.ADDED_TO_STAGE, _init);
        }
        private function _init(e:Event = null):void
        {
            //You may now access the stage property of the DisplayObject.
            stage.addEventListener(Event.RESIZE);
        }
  }

}

Anytime you need access to the stage, have the Class listen for it/check for it in the constructor, and have your init function be the handler.

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    /**
     * ...
     * @author Brian Hodge
     */
    public class SomeClass extends Sprite
    {

        public function SomeClass() 
        {
            if (stage) _init();
            else addEventListener(Event.ADDED_TO_STAGE, _init);
        }
        private function _init(e:Event = null):void
        {
            //You may now access the stage property of the DisplayObject.
            stage.addEventListener(Event.RESIZE);
        }
  }

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