繁体   English   中英

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

[英]Cannot access a property or method of a null object reference

我尝试在Flash中为用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应用程序对按钮按下没有反应

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

在将显示对象添加到显示列表之前, 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);
}

每当您需要访问该阶段时,让Class在构造函数中侦听/检查它,并让您的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);
        }
  }

}

暂无
暂无

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

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