键盘事件 as3 不起作用
这件事让我敲了两个小时才弄清楚。
我决定将其发布在这里,以帮助其他人不要拔毛:)。
本质上,错误是我没有从 Flash 构建器环境中接收键盘事件(在 adobe flash cs5 中也可以看到相同的错误/问题)。我设置了stage.focus = stage,没有帮助。我添加了其他事件侦听器(mouse_down、frame_enter),效果很好,我添加了 MovieClip 子项并侦听这些子项上的事件,但仍然是同样的问题。
package
{
public class Test extends Sprite
{
public function Test()
{
this.addEventListener(Event.ADDED_TO_STAGE,init);
}
public function init(stage:Stage):void
{
this.removeEventListener(Event.ADDED_TO_STAGE,init);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
}
private function keyPressed(e:KeyboardEvent):void
{
trace("keyPressed");
}
private function keyReleased(e:KeyboardEvent):void
{
trace("keyReleased");
}
}
}
This was something that had me banging my head for 2 hours before I figured it out.
I decided to post it here, to help others not pull their hair out :).
Essentially the bug was I was not receiving the keyboard event from within my flash builder environment (The same bug/issue is visible with adobe flash cs5). I set the stage.focus = stage, did not help. I added other event listeners (mouse_down, frame_enter) which worked fine, I added MovieClip children and listened for events on those children, still the same issue.
package
{
public class Test extends Sprite
{
public function Test()
{
this.addEventListener(Event.ADDED_TO_STAGE,init);
}
public function init(stage:Stage):void
{
this.removeEventListener(Event.ADDED_TO_STAGE,init);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
}
private function keyPressed(e:KeyboardEvent):void
{
trace("keyPressed");
}
private function keyReleased(e:KeyboardEvent):void
{
trace("keyReleased");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,请使用
event:Event
而不是stage:Stage
。你需要导入所需的类。
so instead of
stage:Stage
useevent:Event
.and u needs to import needed classes.
标记发生更改的行。顺便说一句,您的代码无法编译,请检查错误日志。
Marked the line that changed. Your code doesnt compile btw, check for error logs.
使用键盘命令需要监听键盘事件。此过程与侦听 AS3 中任何其他事件的过程相同。您需要使用addEventListener()方法来注册KeyboardEvent。然而,与其他对象不同的是,由于键盘不需要附加到项目中的任何特定对象,因此键盘事件通常注册到舞台。在下面的代码中,舞台对象注册每次按下键盘键时触发的键盘事件。
与 AS2 不同,AS3 键盘事件不是全局的。它们被发送到舞台,并通过显示列表冒泡到具有焦点的任何显示对象。
Using keyboard commands requires listening to keyboard events. This process is identical to the process for listening to any other event in AS3. You need to use the addEventListener() method to register with a KeyboardEvent. However, unlike other objects, due to the fact that the keyboard is not necessary attached to any specific object in the project, the Keyboard Event is usually registered with the stage. In the code below, the stage object registers for a keyboard event to be triggered each time a keyboard key is pressed down.
Unlike in AS2, in AS3 Keyboard events are not global. They are issued to the stage, and they bubble through the display list to whatever display object has focus.