官术网_书友最值得收藏!

Accessing raw touchpoint data

Sometimes the predefined gestures that are baked into Flash Player and AIR are not enough for certain application interactions. This recipe will demonstrate how to access raw touch data reported by the operating system through Flash Player or AIR APIs.

How to do it...

To read raw touch data in your project, perform the following steps:

  1. First, import the following classes into your project:
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.display.Stage;
    import flash.display.Sprite;
    import flash.events.TouchEvent;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.ui.Multitouch;
    import flash.ui.MultitouchInputMode;
    
  2. Declare a TextField and TextFormat object to allow visible output upon the device:
    private var traceField:TextField;
    private var traceFormat:TextFormat;
    
  3. We will now set up our TextField, apply a TextFormat, and add it to the DisplayList. 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 = 44;
    traceFormat.align = "left";
    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);
    }
    
  4. Set the specific input mode for the multitouch APIs to support touch input by setting Multitouch.inputMode to the MultitouchInputMode.TOUCH_POINT constant. We will also register a set of listeners for TouchEvent data in the following method:
    protected function setupTouchEvents():void {
    Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
    stage.addEventListener(TouchEvent.TOUCH_MOVE, touchMove);
    stage.addEventListener(TouchEvent.TOUCH_END, touchEnd);
    }
    
  5. To clear out our TextField after each touch interaction ends, we will construct the following function:
    protected function touchEnd(e:TouchEvent):void {
    traceField.text = "";
    }
    
  6. We can then read the various properties from the touch event to interpret in some way. Events such as pressure, coordinates, size, and more can be derived from the event object that is returned:
    protected function touchMove(e:TouchEvent):void {
    traceField.text = "";
    traceField.appendText("Primary: " + e.isPrimaryTouchPoint + "\n");
    traceField.appendText("LocalX: " + e.localX + "\n");
    traceField.appendText("LocalY: " + e.localY + "\n");
    traceField.appendText("Pressure: " + e.pressure + "\n");
    traceField.appendText("SizeX: " + e.sizeX + "\n");
    traceField.appendText("SizeY: " + e.sizeY + "\n");
    traceField.appendText("StageX: " + e.stageX + "\n");
    traceField.appendText("StageY: " + e.stageY + "\n");
    traceField.appendText("TPID: " + e.touchPointID + "\n");
    }
    
  7. The result will appear similar to the following:

How it works...

Each touch point that is registered in the device has a number of specific properties associated with it. By registering a set of listeners to detect these interactions, we can read this data and the application can react appropriately. In our example, we are simply exposing these values via TextField, but this would be the exact data we would need to build a pressure-sensitive gaming mechanic or some other custom gesture.

Note that on a device that allows more than one touchpoint, we will be able to read the data from both touchpoints using the same listener. Multiple touchpoints are differentiated by location on the stage and by touchPointID. We would use these IDs to differentiate between touchpoints when devising complex gestures, or simply when we have the need to keep track of each touchpoint in a precise way.

There's more...

It is important to note that while Multitouch.inputMode is set to MultitouchInputMode.TOUCH_POINT that we will not be able to take advantage of the predefined gestures that Flash Player and AIR make available through the simplified gesture API. Setting the Multitouch.inputMode to MultitouchInputMode.GESTURE will allow us to take advantage of common, predefined gesture events within our application.

主站蜘蛛池模板: 榆社县| 天峨县| 峨眉山市| 六枝特区| 阿巴嘎旗| 开平市| 玛沁县| 综艺| 孟州市| 丰顺县| 井研县| 共和县| 平昌县| 宾阳县| 什邡市| 哈尔滨市| 松原市| 松桃| 改则县| 什邡市| 上杭县| 孙吴县| 甘德县| 方城县| 满洲里市| 长武县| 南平市| 布尔津县| 姚安县| 星座| 曲周县| 翁源县| 囊谦县| 木兰县| 洛浦县| 开化县| 洛隆县| 闽侯县| 阳朔县| 东城区| 无为县|