- Flash Development for Android Cookbook
- Joseph Labrecque
- 435字
- 2021-04-02 19:15:23
Responding to trackball and D-Pad events
Some Android devices have additional physical inputs that we can take advantage of. For instance, the Motorola Droid has a slider keyboard, which includes a directional D-pad and the HTC Nexus One has a built-in trackball control.
How to do it...
We can respond to trackball and D-pad events through standard ActionScript event listeners.
- First, import the following classes into your project:
import flash.display.Shape; import flash.display.Sprite; import flash.display.Stage; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.KeyboardEvent; import flash.text.TextField; import flash.text.TextFormat; import flash.ui.Keyboard;
- Declare a
Shape
alongside aTextField
andTextFormat
object. These will be used for interaction and visual feedback.private var traceField:TextField; private var traceFormat:TextFormat; private var box:Shape;
- We will then set up our
TextField
, apply aTextFormat
, and add it to theDisplayList
. Here, we create a method to perform all of these actions for us:protected function setupTextField():void { traceFormat = new TextFormat(); traceFormat.bold = true; traceFormat.font = "_sans"; traceFormat.size = 32; traceFormat.align = "center"; traceFormat.color = 0x333333; traceField = new TextField(); traceField.defaultTextFormat = traceFormat; traceField.selectable = false; traceField.mouseEnabled = false; traceField.width = stage.stageWidth; traceField.height = stage.stageHeight; addChild(traceField); }
- Next, construct a method to handle the creation of our
Shape
and add it to theDisplayList
.protected function setupBox():void { box = new Shape(); box.graphics.beginFill(0xFFFFFF, 1); box.x = stage.stageWidth/2; box.y = stage.stageHeight/2; box.graphics.drawRect(-100,-100,200,200); box.graphics.endFill(); addChild(box); }
- Set an event listener on the
Stage
to respond to keyboard presses:protected function registerListeners():void { stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown); }
- Now, we simply need to write a switch/case statement that will perform different actions in response to D-pad/trackball events. In this case, we change the position of our
Shape
and output thekeyCode
to aTextField:
protected function keyDown(e:KeyboardEvent):void { var key:uint = e.keyCode; traceField.text = key + " pressed!"; switch(key){ case Keyboard.UP:{ box.y -= 20; break; } case Keyboard.DOWN:{ box.y += 20; break; } case Keyboard.LEFT:{ box.x -= 20; break; } case Keyboard.RIGHT:{ box.x += 20; break; } case Keyboard.ENTER:{ box.x = stage.stageWidth/2; box.y = stage.stageHeight/2; break; } } }
- The result will appear similar to the following:
How it works...
We register listeners for these special controls just as we would the Keyboard.UP, Keyboard.DOWN, Keyboard.LEFT, Keyboard.RIGHT
, and Keyboard.ENTER
keys for any physical keyboard. In this example, we are shifting the target Shape
in each direction and rest the location based upon the D-pad/trackball being pressed. We are also outputting the keyCode
value to a text field.
There's more...
It is important to note that most Android devices do not have such specialized input mechanisms. If we do register events mapped to these keys, we should always supply and alternative as well.
- MATLAB計算機視覺經典應用
- UG NX10.0從新手到高手
- 性能測試從零開始
- 軟件定義數據中心:技術與實踐
- 基于元胞自動機的城市路網交通流建模與仿真
- Photoshop CC中文版基礎與實例教程(第7版)
- 數碼攝影后期處理秘笈:Photoshop CC專業調色(第2版)
- Instant Microsoft SQL Server Analysis Services 2012 Dimensions and Cube
- 邊做邊學:Photoshop+CorelDRAW綜合實訓教程
- Flash Facebook Cookbook
- After Effects CS6入門與提高
- 中文版Photoshop CS6從新手到高手·全彩版
- 中文版Photoshop CS6應用技法教程
- 巧用ChatGPT高效搞定Excel數據分析
- 企業虛擬化實戰:VMware篇