ActionScript 3中的KeyboardEvent不触发 [英] ActionScript 3 KeyboardEvent not firing

查看:208
本文介绍了ActionScript 3中的KeyboardEvent不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的ActionScript开发和正在使用的FlashDevelop IDE。我一直在玩弄一些很简单的事情,所遇到的一个问题,我似乎无法来解决。

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.

我的应用程序编译和运行,以及功能的手表点击事件火灾完美,我可以看到在控制台的事件时,我通过它追踪(),但相同的code窥伺的KeyboardEvent没有开火所有的。

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.

下面是我的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);
        }

    }

}

本的MouseEvent跟踪()触发每一个预期时间,但KeyboardEvent的永远不会触发,不管是什么键我preSS。任何想法?

The MouseEvent trace() fires every time as expected, but KeyboardEvent never fires, no matter what key I press. Any ideas?

推荐答案

您需要听众添加到舞台:

You need to add the listeners to the stage:

private function init(e:Event = null):void
{
    removeEventListener(Event.ADDED_TO_STAGE,init);

    stage.addEventListener(MouseEvent.CLICK, onClickEvent);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownEvent);
}

这篇关于ActionScript 3中的KeyboardEvent不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆