如何使用as3检测鼠标手势

发布时间:2013-3-1浏览:

核心提示:如何使用as3检测鼠标手势

原文: http://www.emanueleferonato.com/2010/07/05/detecting-mouse-gestures-in-flash-with-as3/
原作者: Emanuele Feronato

在计算领域,一个指针设备的手势或者是一个鼠标的手势结合了指针设备的运动和点击,这种手势可以被一些软件识别到并作为一个具体的命令。指针设备的手势可以快速访问一个程序的命令功能。它们也可以方便一些敲打键盘有困难的人士。

例如,在一个浏览器中,用户如果想回到前一个浏览页面,他可以按住鼠标右键并向左拖一小段距离,然后松开右键。我总是先描述我的想法和代码,再展示运行结果。而今天,我会先从结果开始:



左上象限:在这个象限你可以通过鼠标进行简单绘画:按住鼠标然后拖动鼠标。你可以看到,蓝色的线条代表了你的鼠标的运行过程。

右上象限:红色的线条代表了经过过滤之后的鼠标运动。我不想跟踪距离非常短的运动,所以我只会显示距离大于20像素的运动。

左下象限:绿色的线条代表了另一种过滤之后的鼠标运动,我把鼠标的可能运动方向限制在了8个方向:上,下,左,右,和四个对角线。

右下象限:这里用于显示对绿色线条的文字描述,并删除了一些重复的运动,例如像“left left left right”将会变成"left left"。

  1. package {

            import flash.display.Sprite;

            import flash.events.MouseEvent;

            import flash.events.Event;

            import flash.text.TextField;

            public class gesture extends Sprite {

                    public var drawing:Boolean=false;

                    public var freemouse:Sprite=new Sprite();

                    public var stepmouse:Sprite=new Sprite();

                    public var dirmouse:Sprite=new Sprite();

                    public var the_grid:grid=new grid();

                    public var px,py,px2,py2:int;

                    public var directions:TextField=new TextField();

                    public var latest_direction:Number;

                    public function gesture():void {

                            addChild(the_grid);

                            addChild(freemouse);

                            addChild(stepmouse);

                            addChild(dirmouse);

                            addChild(directions);

                            stepmouse.x=250;

                            dirmouse.y=200;

                            directions.x=250;

                            directions.y=200;

                            directions.height=200;

                            addEventListener(Event.ENTER_FRAME,on_enter_frame);

                            stage.addEventListener(MouseEvent.MOUSE_DOWN,on_mouse_down);

                            stage.addEventListener(MouseEvent.MOUSE_UP,on_mouse_up);

                    }

                    public function on_mouse_down(e:MouseEvent):void {

                            if (! drawing) {

                                    directions.text="";

                                    latest_direction=-1;

                                    drawing=true;

                                    freemouse.graphics.clear();

                                    freemouse.graphics.lineStyle(1,0x0000ff);

                                    freemouse.graphics.moveTo(mouseX,mouseY);

                                    stepmouse.graphics.clear();

                                    stepmouse.graphics.lineStyle(1,0xff0000);

                                    stepmouse.graphics.moveTo(mouseX,mouseY);

                                    dirmouse.graphics.clear();

                                    dirmouse.graphics.lineStyle(1,0x00ff00);

                                    dirmouse.graphics.moveTo(mouseX,mouseY);

                                    px=px2=mouseX;

                                    py=py2=mouseY;

                            }

                    }

                    public function on_mouse_up(e:MouseEvent):void {

                            drawing=false;

                    }

                    public function on_enter_frame(e:Event):void {

                            if (drawing) {

                                    freemouse.graphics.lineTo(mouseX,mouseY);

                                    var dx=px-mouseX;

                                    var dy=py-mouseY;

                                    var distance:Number=dx*dx+dy*dy;

                                    if (distance>400) {

                                            stepmouse.graphics.lineTo(mouseX,mouseY);

                                            var angle:Number=Math.atan2(dy,dx)*57.2957795;

                                            var refined_angle:Number;

                                            var string_dir:String;

                                            if (angle>=22*-1&&angle<23) {

                                                    refined_angle=0;

                                                    string_dir="Left\n";

                                            }

                                            if (angle>=23&&angle<68) {

                                                    refined_angle=Math.PI/4;

                                                    string_dir="Up Left\n";

                                            }

                                            if (angle>=68&&angle<113) {

                                                    refined_angle=Math.PI/2;

                                                    string_dir="Up\n";

                                            }

                                            if (angle>=113&&angle<158) {

                                                    refined_angle=Math.PI/4*3;

                                                    string_dir="Up Right\n";

                                            }

                                            if (angle>=135||angle<157*-1) {

                                                    refined_angle=Math.PI;

                                                    string_dir="Right\n";

                                            }

                                            if (angle>=157*-1&&angle<112*-1) {

                                                    refined_angle=- Math.PI/4*3;

                                                    string_dir="Down Right\n";

                                            }

                                            if (angle>=112*-1&&angle<67*-1) {

                                                    refined_angle=- Math.PI/2;

                                                    string_dir="Down\n";

                                            }

                                            if (angle>=67*-1&&angle<22*-1) {

                                                    refined_angle=- Math.PI/4;

                                                    string_dir="Down Left\n";

                                            }

                                            px2-=Math.sqrt(distance)*Math.cos(refined_angle);

                                            py2-=Math.sqrt(distance)*Math.sin(refined_angle);

                                            if (refined_angle!=latest_direction) {

                                                    directions.appendText(string_dir);

                                                    latest_direction=refined_angle;

                                            }

                                            dirmouse.graphics.lineTo(px2,py2);

                                            px=mouseX;

                                            py=mouseY;

                                    }

                            }

                    }

            }

    }

现在你差不多可以在你的代码中使用鼠标手势了......你只要添加一些Levenshtein distance调整就可以大功告成了......我将会在下一次告诉你怎么做,你已经有想法了?


前一篇:定做程序设计
后一篇:右键菜单全屏和退出全屏的互变(as3)
分享到: