为什么不接收KeyboardEvent.KEY_DOWN事件?

时间:2012-09-15 17:36:04

标签: actionscript-3 flex actionscript flash keyboard-events

基本上我只想在我的程序中添加键盘事件监听器,然后开始迭代地构建它们。问题是,即使从多个教程中获取最简单的东西,显然也是一个问题。我不太确定我现在缺少什么。我尝试过添加内容,更改内容以及删除内容。但鉴于以下代码,我没有看到任何消息显示在屏幕上。为什么呢?

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:local="*" xmlns:mx="http://www.adobe.com/2006/mxml" width="480" height="600" layout="absolute" creationComplete="onCreationComplete()">
<mx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import flash.events.KeyboardEvent;

        private function onStart(pEvent:Event):void {
            vs.selectedChild = mm;
        }

        private function onNewGame(pEvent:Event):void {
            vs.selectedChild = game;
        }



        private function onCreationComplete():void {
            addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

            addEventListener(KeyboardEvent.KEY_UP, onKeyDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, onKeyDown);
        }

        private function onKeyDown(pEvent:KeyboardEvent):void {
            Alert.show("spam");
            Alert.show(pEvent.toString());
        }
    ]]>
</mx:Script>
<mx:ViewStack id="vs" creationComplete="vs.selectedChild = ts">
    <local:TitleScreen id="ts" creationComplete="ts.addEventListener(TitleScreen.START, onStart);" />
    <local:MainMenu id="mm" creationComplete="mm.addEventListener(MainMenu.NEW_GAME, onNewGame);" />
    <local:Game id="game" />
</mx:ViewStack>
</mx:Application>

1 个答案:

答案 0 :(得分:1)

您无法直接使用stage。相反,这使用此代码:

    private function onCreationComplete():void {
        systemManager.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
        systemManager.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyDown);
    }