0

所以有人知道为什么它不起作用吗?我想在我的八哥班上听关键事件,我需要集中注意力吗?如果是,我该怎么做?这是一个 Main 类,这是文档类:

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import starling.core.Starling;

public class Main extends Sprite{
    private var starling:Starling

    public function Main(){         
        if (stage)
            init();
        else
            addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
    }

    private function init(e:Event = null):void{
        removeEventListener(Event.ADDED_TO_STAGE, init);                        
        starling = new Starling(ScreenCoorder, stage);
        starling.start();       
    }
}

}

和 screencorder 类(从八哥开始):

package  
{

import starling.display.Sprite;
import starling.events.Event;
import starling.events.KeyboardEvent;

public class ScreenCoorder extends Sprite{

    public function ScreenCoorder() {
        super();
        addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init():void {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        this.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
    }       

    public function onDown(e:KeyboardEvent):void {
        trace("so serious");
    }   
}

}

4

2 回答 2

2

Starling Sprite 不支持按键,您需要像这样将侦听器添加到本机 Stage 对象。

this.nativeStage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
于 2012-09-23T17:43:57.080 回答
1

它不起作用,因为您已将键盘事件侦听器连接到 ScreenCoorder 对象,而不是八哥舞台。试试这个:

private function init():void {
    removeEventListener(Event.ADDED_TO_STAGE, init);
    this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
}

请注意,这是 Starling 阶段,而不是 Flash 阶段。

于 2013-04-01T00:26:28.533 回答