2011-05-24 67 views
0

我正在製作一個簡單的遊戲,我希望能夠在類構造函數中添加鍵盤事件監聽器。但是我有麻煩。我解僱了,你必須通過列出的顯示對象屬性階段通過舞臺?無論如何,我得到的錯誤,不承認我的事件監聽鍵盤事件。通過類AS3將事件監聽器添加到全局舞臺

最佳路易斯

package louiseguchi.game 
{ 
    import flash.display.Stage; 
    import flash.events.Event; 
    import flash.display.Sprite; 
    import flash.ui.Keyboard 
    import flash.events.KeyboardEvent 



    { 

     public function keyBoardVelocity extends EventDispatcher() 
     { 
      // constructor code 

      stage.addEventListener(KeyboardEvent.KEY_UP, keyPressUp); 

     } 
     private static function keyPressUp(Event:KeyboardEvent):void{ 
      trace("ok") 
     } 

    } 

} 

回答

1

這裏的幾個問題。首先,你的班級格式不正確。看到我的評論。

package louiseguchi.game 
    { 
     import Main; //doc class 
     import flash.display.Stage; 
     import flash.events.Event; 
     import flash.display.Sprite; 
     import flash.events.EventDispatcher; 
     import flash.ui.Keyboard 
     import flash.events.KeyboardEvent 

//only classes may be extended 
     public class KeyBoardVelocity extends EventDispatcher 
     { 

    //here is your constructor 
      public function KeyBoardVelocity() 
      { 
       super() 
       init() 
      } 

      private function init():void 
      { 
       addEventHandlers(); 
      } 


      private function addEventHandlers() 
      { 
//static access of main stage 
       Main.stage.addEventListener(KeyboardEvent.KEY_UP, keyPressUp); 
      } 

      // instance function 
      private function keyPressUp(Event:KeyboardEvent):void{ 
       trace("ok") 
      } 

     } 

    } 

請注意,我在構造函數和偵聽器之間添加了幾個步驟。如果你願意,可以這樣做,但我通常在構造函數中很少放置。其次,你需要在顯示列表中有一些對象來訪問舞臺,並且通常通過主文檔類來訪問它。在這裏,我只是在文檔類引用靜態get函數:

package com.b99.testBed //alter as needed 
{ 
    import com.b99.testBed.keypress.KeyBoardVelocity; 
    import flash.display.Sprite; 
    import flash.display.Stage; 

    /** 
    * ... 
    * @author bosworth99 
    */ 
    public class Main extends Sprite 
    { 
     private static var _stage:Stage; 

     public function Main() 
     { 
      super(); 
      init(); 
     } 

     private function init():void 
     { 
      _stage = this.stage; 

      var _candidate:KeyBoardVelocity = new KeyBoardVelocity(); 

     } 

     public static function get stage():Stage { return _stage; } 

    } 

} 

你可以在多種方式獲取階段,這是一個我一般喜歡。

希望有所幫助。

2

您可以創建,你會分配階段參考一個公共的靜態屬性的類,從而使舞臺訪問整個應用程序如以下:

package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 

    public class Main extends Sprite 
    { 
     public function Main():void 
     { 
      if (stage) init(); 
      else addEventListener(Event.ADDED_TO_STAGE, init); 

     }// end function 

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

      GlobalVars.stage = stage; 

      var keyboardVelocity:KeyboardVelocity = new KeyboardVelocity(); 

     }// end function 

    }// end class 

}// end package 

import flash.display.Stage; 

internal class GlobalVars 
{ 
    public static var stage:Stage; 

}// end class 

import flash.events.EventDispatcher; 
import flash.events.KeyboardEvent; 

internal class KeyboardVelocity extends EventDispatcher 
{ 
    public function KeyboardVelocity() 
    { 
     var stage:Stage = GlobalVars.stage; 

     stage.addEventListener(KeyboardEvent.KEY_DOWN, onStageKeyDown); 

    }// end function 

    private function onStageKeyDown(e:KeyboardEvent):void 
    { 
     trace("key down"); 

    }// end function 

}// end class 

注:我用內部類以便您可以簡單地將代碼複製並粘貼到文檔類中並運行它。理想情況下,你會將這些類分離到他們自己的文件中。

就我個人而言,我並不喜歡讓對象在全球範圍內訪問,因爲它構成潛在的安全風險,在我看來是不好的做法。您應該考慮只需在下面的解析階段的引用您的KeyboardVelocity類,如:

package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 

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

      var keyboardVelocity:KeyboardVelocity = new KeyboardVelocity(stage); 

     }// end function 

    }// end class 

}// end package 

import flash.display.Stage; 
import flash.events.EventDispatcher; 
import flash.events.KeyboardEvent; 

internal class KeyboardVelocity extends EventDispatcher 
{ 
    public function KeyboardVelocity(stage:Stage) 
    { 
     stage.addEventListener(KeyboardEvent.KEY_DOWN, onStageKeyDown); 

    }// end function 

    private function onStageKeyDown(e:KeyboardEvent):void 
    { 
     trace("key down"); 

    }// end function 

}// end class 
+0

@Tuarayi - 我同意你關於全局靜態變量,那麼一般。它確實打敗了面向對象模式的目的。這就是說 - 在構造函數中傳遞變量的引用也變得非常粘稠,並且促進了緊密耦合。實際上我並沒有想到在這一點上有一個「正確」的答案,除了「除非真的需要使用全局變量」外,舞臺也是我發現自己經常參考的一個,所以這是一個很好的候選人。 @OP這兩種策略都有效,都有潛在的問題。 – Bosworth99 2011-05-24 22:24:12