2012年10月10日 星期三

as3dpad 操控桿介紹

as3dpad 是作者製作的一個類別庫, 目的在Adobe AIR mobile 提供一個虛擬操控桿, 於iOS 和Android 等流動裝置運作, 為遊戲提供一個輸入方法.

配合Starling 的 [範例] (請以鍵盤WASDm, 操控)

as3dpad 渲染部分參考SpriteSheetClip 的手法, 所以在stage3d 之上有效運作, 與Starling 等框架相容.
另外, 類別庫提供以鍵盤代替螢幕輸入(WASDm,), 給開發人員在電腦直接測試遊戲.

弧度(radians) 表示操控桿位置

以下是編寫的簡單步驟:
  1. 建立DPad 物件
    var dPad:DPad = new DPad();
    this.addChild(dPad);
    
  2. 接收X 軸與Y 軸回傳的數據
    dPad.leftPad.addEventListener(DPadEvent.TOUCH_PRESS, touchPressHandler);
    dPad.leftPad.addEventListener(DPadEvent.TOUCH_RELEASE, touchReleaseHandler);
    
    private function touchPressHandler(event:DPadEvent):void{
      var axisPad:AxisPad = event.target as AxisPad;
      trace("radians: " + axisPad.radians);
      trace("distance: " + axisPad.distance + "/" + axisPad.maxDistance);
      trace("press left:  " + (axisPad.value & AxisPad.LEFT));
      trace("press right: " + (axisPad.value & AxisPad.RIGHT));
      trace("press up:    " + (axisPad.value & AxisPad.UP));
      trace("press down:  " + (axisPad.value & AxisPad.DOWN));
    }
    private function touchReleaseHandler(event:DPadEvent):void{
      ...
    }
    
  3. 接收A 和B 按鈕回傳的數據
    dPad.rightPad.addEventListener(DPadEvent.TOUCH_PRESS_A, touchPressAHandler);
    dPad.rightPad.addEventListener(DPadEvent.TOUCH_PRESS_B, touchPressBHandler);
    dPad.rightPad.addEventListener(DPadEvent.TOUCH_RELEASE_A, touchReleaseAHandler);
    dPad.rightPad.addEventListener(DPadEvent.TOUCH_RELEASE_B, touchReleaseBHandler);
    
    private function touchPressAHandler(event:DPadEvent):void{
      var groupPad:GroupPad = event.target as GroupPad;
      trace("press A: " + (groupPad.value & GroupPad.A_BUTTON));
      trace("press B: " + (groupPad.value & GroupPad.B_BUTTON));
    }
    private function touchPressBHandler(event:DPadEvent):void{
        ...
    }
    private function touchReleaseAHandler(event:DPadEvent):void{
        ...
    }
    private function touchReleaseBHandler(event:DPadEvent):void{
        ...
    }

類別庫提供以下範例參考:
  • example01_basic - 基本用法 [demo]
  • example02_custom_UI - 自定介面 [demo]
  • example03_double_AxisPad - 雙操控桿 [demo]
  • example04_touch9Grid - 螢幕九格觸控 [demo]
  • example05_starling - 配合Starling [demo]

Launch Examples Launch Examples Launch Examples Launch Examples Launch Examples

大家可以 [下載] 範例試試看.

沒有留言: