簡體   English   中英

為什么我的KEY_DOWN事件沒有觸發?

[英]Why doesn't my KEY_DOWN event fire?

我是ActionScript的新手,並且按照這里的教程開始使用FlashDevelop。 我將其設置為每次KEY_DOWN事件移動項目而不是使用frame事件,但我的KEY_DOWN事件從未被觸發。 我快速添加了一個MOUSE_DOWN事件來測試有效問題的程度。

我正在添加監聽器,因為我的Paddle(Sprite的擴展)項目被添加到舞台:

private function addedToStage(e:Event) : void {
    // Remove this event listener
    removeEventListener(Event.ADDED_TO_STAGE, addedToStage);

    // Add the picture
    addChild(pic);

    // Set the location of the item in the middle of the screen one
    // height length of the image down.
    this.y = this.height;
    this.x = (this.stage.stageWidth - this.width) / 2;

    // Add the keyboard listener (broken).
    stage.addEventListener(KeyboardEvent.KEY_DOWN, this.keyDownHandler, false, 0, true);

    // Add the mouse listener (works).
    stage.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseDown, false, 0, true);

    // Set the focus to the stage.
    stage.focus = stage;
}

我的keyDownHandler函數中有一個斷點,它永遠不會被調用。

這可能是這個問題的重復,但我已經在做那個問題中的答案所描述的內容,並在評論中他解釋說他不知道是什么解決了問題,只是突然開始工作。

這個問題的另一個流行答案提到了這篇文章 ,這就是為什么我的代碼中有一行將焦點重新設置到舞台上。 我的舞台上沒有別的東西。

可能會發生什么?

編輯:

這是我的聽眾代碼:

private function keyDownHandler(e:KeyboardEvent):void {
    var increment:Number; // Here is my breakpoint
    if (e.keyCode == 65 || e.keyCode == 37) {
        increment = -5;
    } else if(e.keyCode == 68 || e.keyCode == 39) {
        increment = 5;
    }
    var newX:Number = this.x + 5;
    if (newX > 0 && newX + this.width < this.stage.width) {
        this.x = newX;
    }
}

問題與事件本身無關,而是我如何測試它是否被解雇。 您不能在聲明上放置斷點。

FlashDevelop將允許您在那里設置斷點,但調試器實際上不會在此時停止。

移動擋板的代碼不起作用(此時對我來說並不奇怪),但是如果我將斷點移動到if語句中的行(或執行實際操作時的任何其他行,例如跟蹤),它會突然斷開精細。 現在我想起來,即使我使用過的其他語言都有這種行為,我對IDE的過於寬容,會在這種情況下將斷點移動到最近的可用行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM