KeyboardEvent Listener 导致 TypeError?

发布于 2025-01-03 20:52:04 字数 1221 浏览 3 评论 0原文

我收到这个奇怪的错误,不知道为什么,我已将代码减少到最少并找到了问题的根源,但我不明白为什么会出错。

Main.as

package  {
    import flash.display.MovieClip;
    public class main extends MovieClip{
        public function main(){
            var player1 = new playerTest( 100 , 275 );
            addChild( player1 );
        }
    }
}

Player.as

package {
    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;

    public class playerTest extends MovieClip{
        public function playerTest(xCoord:int, yCoord:int){
            x = xCoord;
            y = yCoord;
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownEvent);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUpEvent);
        }
        private function keyDownEvent(e:KeyboardEvent):void{
            //
        }
        private function keyUpEvent(e:KeyboardEvent):void{
            //
        }
    }
}

如果我注释掉其中一个 KeyboardEvent 侦听器,那么它可以正常工作,但是只要将它们放在那里就会出错并返回

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at playerTest()
    at main()

这只是在我转换代码以通过代码生成我的角色后才开始发生的而不是直接把它扔到舞台上,在它完全正常工作之前是这样的。

I'm getting this weird error, don't know why, I've reduced the code to minimum and found the source of the problem but I don't understand why it errors.

Main.as

package  {
    import flash.display.MovieClip;
    public class main extends MovieClip{
        public function main(){
            var player1 = new playerTest( 100 , 275 );
            addChild( player1 );
        }
    }
}

Player.as

package {
    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;

    public class playerTest extends MovieClip{
        public function playerTest(xCoord:int, yCoord:int){
            x = xCoord;
            y = yCoord;
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownEvent);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUpEvent);
        }
        private function keyDownEvent(e:KeyboardEvent):void{
            //
        }
        private function keyUpEvent(e:KeyboardEvent):void{
            //
        }
    }
}

If I comment out either of the KeyboardEvent listeners then it works fine, but just having them there like that makes it error and returns

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at playerTest()
    at main()

This only has started happening since I converted my code to make my character be spawned through code instead of just throwing it onto the stage, when it was like that before it worked totally fine.

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

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

发布评论

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

评论(3

歌枕肩 2025-01-10 20:52:04

实际上,您可以从舞台上已有的任何 DisplayObject 访问舞台。因此,在您的班级中,您可以等到播放器添加到舞台后添加键盘侦听器,如下所示:

package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;

public class playerTest extends MovieClip{
    public function playerTest(xCoord:int, yCoord:int){
        x = xCoord;
        y = yCoord;
        addEventListener(Event.ADDED_TO_STAGE,init);

    }
    private function init(e:Event)
    {
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownEvent);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyUpEvent);
    }
    private function keyDownEvent(e:KeyboardEvent):void{
        //
    }
    private function keyUpEvent(e:KeyboardEvent):void{
        //
    }
}
}

Actually, you can access stage from any DisplayObject that is already on the stage. So, in your class, you can wait till the player is added to the stage to add the keyboard listeners, like this:

package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;

public class playerTest extends MovieClip{
    public function playerTest(xCoord:int, yCoord:int){
        x = xCoord;
        y = yCoord;
        addEventListener(Event.ADDED_TO_STAGE,init);

    }
    private function init(e:Event)
    {
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownEvent);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyUpEvent);
    }
    private function keyDownEvent(e:KeyboardEvent):void{
        //
    }
    private function keyUpEvent(e:KeyboardEvent):void{
        //
    }
}
}
沩ん囻菔务 2025-01-10 20:52:04

与 Benny 所说的相反,您绝对可以使用外部类中的舞台,但是该类必须是也在舞台上的 DisplayObject(或其子类)。当player1被实例化然后添加到舞台时,它还没有对舞台的引用。您可以侦听 Event.ADDED_TO_STAGE,然后根据需要将事件连接到舞台。

Contrary to what Benny says, you can absolutely use the stage from an external class, however that class must be a (or a subclass of a) DisplayObject that is also -on- the stage. As player1 is instantiated and then added to the stage, it does not yet have a reference to the stage. You can listen for Event.ADDED_TO_STAGE and then hookup your events to the stage if you'd like.

冷夜 2025-01-10 20:52:04

从外部类引用 stage 并不是最佳实践,如果您没有对任何 displayobject 进行子类化,则可能不允许您引用 stage< /代码>。所以最好使用displayObjects。

package {
        import flash.display.MovieClip;
        import flash.events.KeyboardEvent;
        import flash.display.DisplayObject;

        public class playerTest extends MovieClip{
            public function playerTest(xCoord:int, yCoord:int,_stage:DisplayObject){
                x = xCoord;
                y = yCoord;
                _stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownEvent);
                _stage.addEventListener(KeyboardEvent.KEY_UP, keyUpEvent);
            }
            private function keyDownEvent(e:KeyboardEvent):void{
                //
                trace("down");
            }
            private function keyUpEvent(e:KeyboardEvent):void{
                //
            }
        }
    }

//主要的

 package  {
        import flash.display.MovieClip;
        public class main extends MovieClip{
            public function main(){
                var player1 = new playerTest( 100 , 275 ,stage);
                addChild( player1 );
            }
        }
    }

It is not a best practice to refer the stage from an external class, may be if you are not subclassing any displayobject that will not allow you to refer stage. So it is better to go with displayObjects.

package {
        import flash.display.MovieClip;
        import flash.events.KeyboardEvent;
        import flash.display.DisplayObject;

        public class playerTest extends MovieClip{
            public function playerTest(xCoord:int, yCoord:int,_stage:DisplayObject){
                x = xCoord;
                y = yCoord;
                _stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownEvent);
                _stage.addEventListener(KeyboardEvent.KEY_UP, keyUpEvent);
            }
            private function keyDownEvent(e:KeyboardEvent):void{
                //
                trace("down");
            }
            private function keyUpEvent(e:KeyboardEvent):void{
                //
            }
        }
    }

//main

 package  {
        import flash.display.MovieClip;
        public class main extends MovieClip{
            public function main(){
                var player1 = new playerTest( 100 , 275 ,stage);
                addChild( player1 );
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文