按键未触发 KeyboardEvent

发布于 2024-11-19 04:34:33 字数 754 浏览 1 评论 0原文

我正在开始一个简单的乒乓球游戏,并且创建了一个尚未执行任何操作的 Paddle 类。但是,我认为它不起作用。

package  
{
    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;
    public class Paddle extends MovieClip
    {
            private var paddleSpeed:int = 4;

            public function Paddle() 
            {
                trace("hello!")
                addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
            }

            public function keyDown(e:KeyboardEvent):void
            {
                trace(e.keyCode);
            }

    }

}

在 Main.as 中,我这样做了:

var player:Paddle = new Paddle;
addChild(player);

当我运行代码时,我得到 hello!,但是当我按任意键时什么也没有发生。我读过 KeyboardEvent 教程,并且我正在做他们正在做的事情。感谢您的帮助

I'm starting a simple pong game, and I created a Paddle class that doesn't do anything yet. However, I don't think it is working.

package  
{
    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;
    public class Paddle extends MovieClip
    {
            private var paddleSpeed:int = 4;

            public function Paddle() 
            {
                trace("hello!")
                addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
            }

            public function keyDown(e:KeyboardEvent):void
            {
                trace(e.keyCode);
            }

    }

}

In Main.as I have done this:

var player:Paddle = new Paddle;
addChild(player);

When I run the code, I get hello!, but when I press any key nothing happens. I've read a KeyboardEvent tutorial, and I'm doing what they are doing. Thanks for any help

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

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

发布评论

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

评论(2

轮廓§ 2024-11-26 04:34:33

尝试将事件侦听器添加到 stage 而不是 Paddle。另外,当您测试项目时,请确保禁用键盘快捷键。

在此处输入图像描述

我实际上通常做的是使用 创建一个 Keyboard 类我可以在整个应用程序中引用 down() 函数。它的用法是这样的:

if(Keyboard.down(65, 67))
{
    trace("A and/or C are being held down!");
}

Try adding the event listener to stage instead of Paddle. Also, when you're testing your project make sure you have keyboard shortcuts disabled.

enter image description here

What I actually normally do is make a Keyboard class with a down() function that I can reference throughout the application. Its use would be something like this:

if(Keyboard.down(65, 67))
{
    trace("A and/or C are being held down!");
}
灯角 2024-11-26 04:34:33

KeyboardEvent.KEY_DOWN 由焦点中的物体触发。空剪辑无法对焦。

适合您情况的可靠方法是订阅 stage。

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);

KeyboardEvent.KEY_DOWN is triggered by something in focus. Empty clip can not be in focus.

Reliable way for your situation is to subscribe to stage.

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