ActionScript 3 KeyboardEvent 未触发
我是 ActionScript 开发新手,正在使用 FlashDevelop IDE。我一直在尝试一些非常简单的事情,并遇到了一个我似乎无法解决的问题。
我的应用程序编译并运行,并且监视单击事件的函数完美触发,当我将其传递给trace() 时,我可以在控制台中看到该事件,但监视 KeyboardEvent 的相同代码根本无法触发。
这是我的代码:
package GameTesting
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
[Frame(factoryClass="GameTesting.Preloader")]
public class Main extends Sprite
{
public function Main():void
{
if (stage) {
init();
} else {
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE,init);
addEventListener(MouseEvent.CLICK, onClickEvent);
addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownEvent);
}
private function onKeyDownEvent(e:KeyboardEvent):void
{
trace(e);
}
private function onClickEvent(e:MouseEvent):void
{
trace(e);
}
}
}
MouseEvent trace() 每次都会按预期触发,但 KeyboardEvent 永远不会触发,无论我按哪个键。有什么想法吗?
I'm new to ActionScript development and am using the FlashDevelop IDE. I've been playing around with some really simplistic things and have come across a problem I can't seem to solve.
My application compiles and runs, and a function that watches click events fires perfectly and I can see the event in the console when I pass it to trace(), yet the same code watching for KeyboardEvent fails to fire at all.
Here's my code:
package GameTesting
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
[Frame(factoryClass="GameTesting.Preloader")]
public class Main extends Sprite
{
public function Main():void
{
if (stage) {
init();
} else {
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE,init);
addEventListener(MouseEvent.CLICK, onClickEvent);
addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownEvent);
}
private function onKeyDownEvent(e:KeyboardEvent):void
{
trace(e);
}
private function onClickEvent(e:MouseEvent):void
{
trace(e);
}
}
}
The MouseEvent trace() fires every time as expected, but KeyboardEvent never fires, no matter what key I press. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将侦听器添加到舞台:
You need to add the listeners to the stage:
您可能需要确保监听事件的影片剪辑具有焦点。如果您的游戏有某种菜单或开始屏幕,您可以单击按钮来启动,则该影片剪辑可以保留焦点,即使它已从舞台上删除。
这是一篇很好的文章,更详细地解释了这一点:
键盘舞台上的活动不起作用...?
You may need to be sure the movieclip listening for the event has focus. If you have some sort of menu or start screen for your game that you click a button on to start, that movieclip can retain focus even if it has been removed from stage.
Here's a good article explaining this in further detail:
Keyboard events on stage are not working...?
如果您使用 Flash IDE(至少对于 CS4),您还需要在调试 (Ctrl+Shift+Enter) 或测试 (Ctrl+Enter) fla 时禁用键盘快捷键。
在电影窗口中,选择“控制”->“禁用键盘快捷键”。
这将停止 Flash IDE 接收键盘输入,并允许调试/测试窗口接收键盘输入。
If you are in the Flash IDE (for CS4 at least) you will also need to disable the keyboard shortcuts when you debug (Ctrl+Shift+Enter) or test (Ctrl+Enter) the fla.
In the movie window, select 'Control'->'Disable Keyboard Shortcuts'.
This will stop the Flash IDE recieving the keyboard input, and allow the debug/test window to instead.