Flash Android移动触摸传感器使用教程

来源:互联网 发布:改号码的软件 编辑:程序博客网 时间:2024/05/22 04:48
技术点:
        1.多点触摸
        2.手势识别
        3.加速度感应
        4.全球定位


开发环境:FlashBuilder Burrito
调试环境:android手机,用usb连接,启用调试模式。(注:模拟器无法支持:>_<:)
Demo截图-->
首先建立一个基本的Demo模版
界面: 左边竖排4个按钮分别对应4个Demo。
          按钮下输出调试信息。
          右边为Demo展示区域。

代码如下:
package
{
        import cx.teach.phone.*;
        import cx.ui.components.Button;
        

        import flash.events.MouseEvent;
        
        import net.hires.debug.Logger;
        
        /**
         * Flash移动平台触摸和传感器的使用Demo
         * @author 翼翔天外
         * 
         */
        public class TeachFlashForPhone extends Sprite
        {
                /**
                 * 指向当前Demo 
                 */
                private var _nowDemo:DisplayObject;
                
                public function TeachFlashForPhone()
                {
                        super();
                        init();
                }
                
                private function init():void
                {
                        //首先要设置舞台为左上对齐和不拉伸
                        stage.align = StageAlign.TOP_LEFT;
                        stage.scaleMode = StageScaleMode.NO_SCALE;
                        
                        //Demo按钮列表
                        new Button(this,0,0,"多点触摸",onMultitouchClick);
                        new Button(this,0,70,"手势识别",onGestureClick);
                        new Button(this,0,140,"加速度感应",onAccelerometerClick);
                        new Button(this,0,210,"全球定位",onGeolocationClick);
                        
                        //输出信息在界面上
                        var log:Logger = new Logger();
                        log.y = 280;
                        addChild(log);
                }
                
                /**
                 * 切换Demo
                 * @param demo
                 * 
                 */
                private function changeDemo(demo:DisplayObject):void
                {
                        if(_nowDemo)
                        {
                                removeChild(_nowDemo);
                        }
                        _nowDemo = demo;
                        addChild(_nowDemo);
                }
                
                private function onMultitouchClick(e:MouseEvent):void
                {
                        Logger.clear();
                        Logger.info("多点触摸Demo");
                        changeDemo(new MultitouchDemo);
                }
                
                private function onGestureClick(e:MouseEvent):void
                {
                        Logger.clear();
                        Logger.info("手势识别Demo");
                        changeDemo(new GestureDemo);
                }
                
                private function onAccelerometerClick(e:MouseEvent):void
                {
                        Logger.clear();
                        Logger.info("加速度感应Demo");
                        changeDemo(new AccelerometerDemo);                        
                }
                
                private function onGeolocationClick(e:MouseEvent):void
                {
                        Logger.clear();
                        Logger.info("全球定位Demo");
                        changeDemo(new GeolocationDemo);        
                }                
        }
}


参看工程主类:TeachFlashForPhone 

Demo基本方法说明
每个Demo 都有init和destroy方法,分别对应添加到舞台的初始化操作和移出舞台时的释放操作。

1.        多点触摸        
代码参看:cx.teach.phone. MultitouchDemo
范例功能:建立两个小球,可以用两个手指同时拖动,并绘制拖动轨迹。
代码如下:

package cx.teach.phone
{
import cx.teach.phone.display.Ball;


import flash.display.Sprite;
import flash.events.Event;
import flash.events.TouchEvent;
import flash.geom.Point;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.utils.Dictionary;
import flash.www.yqu8.com;
import flash.www.lku8.com;
import flash.www.hxp8.com;
import flash.www.jzc8.com;


import net.hires.debug.Logger;


/**
* 多点触摸Demo 
* @author 翼翔天外

*/
public class MultitouchDemo extends Sprite
{
private var _ball1:Ball;
private var _ball2:Ball;
/**
* 记录拖动的手指 
*/
private var _touchDic:Dictionary = new Dictionary();


public function MultitouchDemo()
{
addEventListener(Event.ADDED_TO_STAGE,init);
addEventListener(Event.REMOVED_FROM_STAGE,destroy);
}


/**
* 初始化 

*/
public function init(e:Event = null):void
{
//判断是否支持
if(!Multitouch.supportsTouchEvents)
{
Logger.error("不支持多点触摸!");
return;
}
//添加多点支持
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
//横向排列两个球
_ball1 = new Ball();
_ball1.x = 300;
_ball1.y = 100;
addChild(_ball1);
_ball2 = new Ball();
_ball2.x = 500;
_ball2.y = 100;
addChild(_ball2);
//添加触摸事件
_ball1.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
_ball2.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
stage.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
stage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);
}


/**
* 销毁 

*/
public function destroy(e:Event = null):void
{
if(_ball1)_ball1.removeEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
if(_ball2)_ball2.removeEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
stage.removeEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
stage.removeEventListener(TouchEvent.TOUCH_END, onTouchEnd);
}


/**
* 触摸开始 
* @param e

*/
private function onTouchBegin(e:TouchEvent):void
{
//开始拖拽,并绑定到一个手指
Sprite(e.currentTarget).startTouchDrag(e.touchPointID);
//记录当前手指到字典
_touchDic[e.touchPointID] = new Point(e.stageX,e.stageY);
Logger.info("Begin TouchPointId:" + e.touchPointID);
}


/**
* 触摸移动中 
* @param e

*/
private function onTouchMove(e:TouchEvent) :void
{
var oldPos:Point = _touchDic[e.touchPointID];
var newPos:Point = new Point(e.stageX,e.stageY);
//线条跟随
drawLine(oldPos,newPos,0);
//更新手指位置
_touchDic[e.touchPointID] = newPos;
}


/**
* 触摸结束 
* @param e

*/
private function onTouchEnd(e:TouchEvent):void
{
Logger.info("End TouchPointId:" + e.touchPointID);
delete _touchDic[e.touchPointID]; 
//停止拖拽
Sprite(e.currentTarget).stopTouchDrag(e.touchPointID);


}


/**
* 绘制线段 
* @param fromPos
* @param toPos
* @param color

*/
private function drawLine(fromPos:Point,toPos:Point,color:uint):void
{
this.graphics.lineStyle(1,color);
this.graphics.moveTo(fromPos.x,fromPos.y);
this.graphics.lineTo(toPos.x,toPos.y);
}
}
}
代码说明:
首先是要做一个可行性检测
if(!Multitouch.supportsTouchEvents)
{
      Logger.error("不支持多点触摸!");
      return;
}
不支持就没戏了,可能要做些友好提示,或切换到其他操作模式。
然后这一句就是告诉手机我要使用多点触摸了,要自己来用代码来判断触摸点。
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
我们需要监听TouchEvent中的三个事件(TOUCH_BEGIN,TOUCH_MOVE,TOUCH_END)
分别对应触摸的开始,移动和结束。相当于鼠标的MOUSE_DOWN,MOUSE_MOVE,MOUSE_UP。
开始和停止拖动:
startTouchDrag(e.touchPointID); stopTouchDrag(e.touchPointID);
注意:在拖放中传入了一个参数e.touchPointID,它是flash为每个触摸点分配的唯一的值,就是对应你当前在屏幕上的那个手指。在每个事件中都通过这个Id来判断。以前操作鼠标因为只有一个点,所以不用加判断。这里有多个点,那么我们就要用一个字典来管理他们,我建立了一个私有变量_touchDic来管理和保存某个触摸点上次的位置坐标。这样可以根据每个手指做更加复杂的处理。
然后在移动的时候我更新了字典中触摸点的位置,做了画线处理,当然还可以做更加有趣的操作,有待创意了。
大致就是这样的一个流程,代码里面应该写得比较清晰了。

2.        手势识别
代码参看:cx.teach.phone. GestureDemo
范例功能:建立一个方块,用手指在屏幕上做手势来旋转,缩放,移动。
代码如下:
package cx.teach.phone
{
import cx.teach.phone.display.Box;


import flash.display.Sprite;
import flash.events.Event;
import flash.events.GesturePhase;
import flash.events.TransformGestureEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;


import net.hires.debug.Logger;


/**
* 手势识别Demo 
* @author 翼翔天外

*/
public class GestureDemo extends Sprite
{
private var _box:Box;


public function GestureDemo()
{
addEventListener(Event.ADDED_TO_STAGE,init);
addEventListener(Event.REMOVED_FROM_STAGE,destroy);
}


/**
* 初始化 

*/
public function init(e:Event = null):void
{
//判断是否支持
if(!Multitouch.supportsGestureEvents)
{
Logger.error("不支持手势识别!");
return;
}
//添加手势支持
Multitouch.inputMode = MultitouchInputMode.GESTURE;
//建立方块
_box = new Box();
_box.x = 400;
_box.y = 200;
addChild(_box);
//添加手势监听
stage.addEventListener(TransformGestureEvent.GESTURE_ROTATE,onRotate);//旋转:用两根手指做旋转手势
stage.addEventListener(TransformGestureEvent.GESTURE_ZOOM,onZoom);//缩放:用两根手指靠近和远离
stage.addEventListener(TransformGestureEvent.GESTURE_PAN,onPan);//平移:用两根手指在屏幕上滑动
}


/**
* 销毁 

*/
public function destroy(e:Event = null):void
{
stage.removeEventListener(TransformGestureEvent.GESTURE_ROTATE,onRotate);
stage.removeEventListener(TransformGestureEvent.GESTURE_ZOOM,onZoom);
stage.removeEventListener(TransformGestureEvent.GESTURE_PAN,onPan);
}


/**
* 识别为旋转 
* @param e

*/
private function onRotate(e:TransformGestureEvent):void

_box.rotation += e.rotation;
switch(e.phase)
{
case GesturePhase.BEGIN:
Logger.info("旋转");
}
}


/**
* 识别为缩放 
* @param e

*/
private function onZoom(e:TransformGestureEvent):void

_box.scaleX *= e.scaleX;
_box.scaleY *= e.scaleY;
switch(e.phase)
{
case GesturePhase.BEGIN:
Logger.info("缩放");
}
}


/**
* 识别为移动 
* @param e

*/
private function onPan(e:TransformGestureEvent):void
{


_box.x += e.offsetX;
_box.y += e.offsetY;
//手势事件的阶段
switch(e.phase)
{
case GesturePhase.BEGIN:
Logger.info("移动");
_box.alpha = .5;
break;
case GesturePhase.UPDATE:


break;
case GesturePhase.END:
_box.alpha = 1;
break;
}
}


}
}

出处:乐酷网络

原创粉丝点击