Starling keyevents

时间:2012-09-23 12:03:07

标签: flash actionscript listener keyevent starling-framework

所以任何人都知道它为什么不起作用?我想用我的starling类听我的关键事件,我是否需要关注,如果是的话我该怎么做?这是一个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();       
    }
}

}

和屏幕录制类(以starling开头):

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");
    }   
}

}

2 个答案:

答案 0 :(得分:2)

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

this.nativeStage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);

答案 1 :(得分:1)

它不起作用,因为您已将键盘事件侦听器挂钩到ScreenCoorder对象,而不是starling阶段。试试这个:

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

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