按键未触发 KeyboardEvent
我正在开始一个简单的乒乓球游戏,并且创建了一个尚未执行任何操作的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将事件侦听器添加到
stage
而不是Paddle
。另外,当您测试项目时,请确保禁用键盘快捷键。我实际上通常做的是使用
创建一个
函数。它的用法是这样的:Keyboard
类我可以在整个应用程序中引用 down()Try adding the event listener to
stage
instead ofPaddle
. Also, when you're testing your project make sure you have keyboard shortcuts disabled.What I actually normally do is make a
Keyboard
class with adown()
function that I can reference throughout the application. Its use would be something like this:KeyboardEvent.KEY_DOWN
由焦点中的物体触发。空剪辑无法对焦。适合您情况的可靠方法是订阅 stage。
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.