在前面提到过,Starling是Sparrow的姊妹篇,正因为这样,Starling里的touch事件的机制其实是为移动设备的触摸交互设计的,所以当你使用它进行使用鼠标交互的桌面应用开发时,第一眼会感觉有些困惑。

首先,如果你看一下starling的类结构图的话,你会发现starling和本地显示列表结构不同的地方在于它没有InteractiveObject类(InteractiveObject 类是用户可以使用鼠标和键盘与之交互的所有显示对象的抽象基类),所有的显示对象使用默认的交互,换句话说,在displayobject中定义了这些交互行为(starling其实从本地stage中注册事件,然后通过自己的结构将本地MouseEvent和TouchEvent结合成为它自己的独有的TouchEvent,这样开发桌面应用时,基本不用做什么修改就可以移植到移动平台了,大家知道的,现在平板电脑正在疯狂普及呐!)。

我们在前面已经用到过touch事件了。我们从最基本的东西开始,比如捕捉当鼠标触碰到quad时触发的事件。为了实现这点我们使用 TouchEvent.TOUCH事件:

1 // when the sprite is touched 2 _customSprite.addEventListener(TouchEvent.TOUCH, onTouchedSprite); 

你会不会认为这是一个相当有限的功能?事实上它是非常强大的,因为你可以从这个单一的事件里得到许多不同的状态。每当鼠标或手指和图形对象发生交互时,TouchEvent.TOUCH就会被触发。

让我们在进一步看看下面的代码,我们在onTouch方法里trace了一下Touch对象的phase属性:

 1 private function onTouch (e:TouchEvent):void  2 {  3   // get the mouse location related to the stage  4     var touch:Touch = e.getTouch(stage);  5     var pos:Point = touch.getLocation(stage);  6   7     trace ( touch.phase );  8   9     // store the mouse coordinates 10     _mouseY = pos.y; 11     _mouseX = pos.x; 12 } 

当我们开始和一个quad交互直到点击了它,这个过程会触发不同的phase。下面是TouchPhase类中所有phase的常量属性:

1 •  began : A mouse or finger starts interacting (similar to a mouse down state). 2 •  ended : A mouse or finger stop interacting (similar to a native click state). 3 •  hover : A mouse or finger is hovering an object. (similar to a native mouse over state) 4 •  moved : A mouse or finger is moving an object (similar to a native  mouse down state + a mouse move state). 5 •  stationary : A mouse or finger stopped interactng with an object and stays over it. 

我们再来看看TouchEvent类的其他一些常用的属性:

1 •  ctrlKey : A boolean returning the state of the ctrl key (down or not). 2 •  getTouch: Gets the first Touch object that originated over a certain target and are in a certain phase. 3 •  getTouches : Gets a set of Touch objects that originated over a certain target and are in a certain phase. 4 •  shiftKey: A boolean returning the state of the shift key (down or not). 5 •  timestamp : The time the event occurred (in seconds since application launch). 6 •  touches : All touches that are currently happening. 

在使用键盘组合键的时候shiftKey和ctrlKey属性就非常有用了。综上,每当有交互发生不论是鼠标还是手指,都会有一个Touch事件相关联。

让我们来看看Touch类的pai:

 1 •  clone : Clones the object.  2 •  getLocation: Converts the current location of a touch to the local coordinate system of a display object.  3 •  getPreviousLocation: Converts the previous location of a touch to the local coordinate system of a display  4 object.  5 •  globalX: The x-position of the touch in screen coordinates.  6 •  globalY : The y-position of the touch in screen coordinates.  7 •  id: A unique id for the object.  8 •  phase : The current phase the touch is in.  9 •  previousGlobalX : The previous x-position of the touch in screen coordinates. 10 •  previousGlobalY : The previous y-position of the touch in screen coordinates 11 •  tapCount : The number of taps the finger made in a short amount of time. Use this to detect double-taps, etc. 12 •  target : The display object at which the touch occurred. 13 •  timestamp : The moment the event occurred (in seconds since application start). 14  

模拟多点触摸

当进行移动设备的开发时,你会有很多时候想要使用多点触摸的交互操作,比如放大缩小图片。当你再台式电脑上进行开发时,手里没有移动设备,这时你就会需要使用到Starling内建的一个很好的模拟多点触摸的机制。

使用这个机制需要你将Starling类的simulateMultiTouch属性设置为true:

package{     import flash.display.Sprite;     import flash.display.StageAlign;     import flash.display.StageScaleMode;     import starling.core.Starling; 

    [SWF(width="1280", height="752", frameRate="60", backgroundColor="#002143")]     public class Startup extends Sprite     {         private var mStarling:Starling; 

        public function Startup()         {            // stats class for fps       addChild ( new Stats() ); 

      stage.align = StageAlign.TOP_LEFT;       stage.scaleMode = StageScaleMode.NO_SCALE; 

      // create our Starling instance               mStarling = new Starling(Game, stage);       // emulate multi-touch       mStarling.simulateMultitouch = true;              // set anti-aliasing (higher the better quality but slower performance)              mStarling.antiAliasing = 1;              // start it!       mStarling.start();         }     } } 

按照上面的代码进行设置之后,按住ctrl键,这时会出现两个小圆圈来模拟多点触摸,下面的插图说明了这一点:

在下面的代码中,我们将使用模拟多点触摸的小圆点来将一个quad进行形变,就像使用两个手指一样。

我们取出两个触摸点并计算它们之间的距离:

 1 package   2 {  3     import flash.geom.Point;  4   5     import starling.display.Sprite;  6     import starling.events.Event;  7     import starling.events.Touch;  8     import starling.events.TouchEvent;  9     import starling.events.TouchPhase; 10  11     public class Game extends Sprite 12     { 13     private var customSprite:CustomSprite; 14      15         public function Game() 16         {   17       addEventListener(Event.ADDED_TO_STAGE, onAdded); 18         } 19  20     private function onAdded ( e:Event ):void 21     { 22       // create the custom sprite 23       customSprite = new CustomSprite(200, 200); 24        25       // positions it by default in the center of the stage 26       // we add half width because of the registration point of the custom sprite (middle) 27       customSprite.x = (stage.stageWidth - customSprite.width >> 1 ) + (customSprite.width >> 1); 28       customSprite.y = (stage.stageHeight - customSprite.height >> 1) + (customSprite.height >> 1); 29        30       // show it 31       addChild(customSprite); 32  33       // we listen to the mouse movement on the stage 34       //stage.addEventListener(TouchEvent.TOUCH, onTouch); 35       // need to comment this one ? ;) 36       stage.addEventListener(Event.ENTER_FRAME, onFrame); 37       // when the sprite is touched 38       customSprite.addEventListener(TouchEvent.TOUCH, onTouchedSprite); 39     } 40  41     private function onFrame (e:Event):void 42     { 43       // we update our custom sprite 44       customSprite.update(); 45     } 46  47     private function onTouchedSprite(e:TouchEvent):void 48     { 49             // retrieves the touch points 50             var touches:Vector.<Touch> = e.touches; 51  52             // if two fingers 53             if ( touches.length == 2 ) 54             { 55                 var finger1:Touch = touches[0]; 56                 var finger2:Touch = touches[1]; 57  58                 var distance:int; 59                 var dx:int; 60                 var dy:int; 61  62                 // if both fingers moving (dragging) 63                 if ( finger1.phase == TouchPhase.MOVED && finger2.phase == TouchPhase.MOVED ) 64                 { 65                     // calculate the distance between each axes 66                     dx = Math.abs ( finger1.globalX - finger2.globalX ); 67                     dy = Math.abs ( finger1.globalY - finger2.globalY ); 68                      69                     // calculate the distance 70                     distance = Math.sqrt(dx*dx+dy*dy); 71  72                     trace ( distance ); 73                 } 74             } 75     } 76     } 77 } 

下一篇我们进行如何操作纹理(texture)的讲解。

[转]starling教程-触摸事件(Touch Events)(四)的更多相关文章

  1. JS事件监听手机屏幕触摸事件 Touch

    JS移动客户端--触屏滑动事件 移动端触屏滑动的效果其实就是图片轮播,在PC的页面上很好实现,绑定click和mouseover等事件来完成.但是在移动设备上,要实现这种轮播的效果,就需要用到核心的t ...

  2. javascript触摸事件touch使用

    详细内容请点击 Apple在iOS 2.0中引入了触摸事件API,Android正迎头赶上这一事实标准,缩小差距.最近一个W3C工作组正合力制定这一触摸事件规范.        在本文深入研究iOS和 ...

  3. 触摸事件 Touch MotionEvent ACTION

    MotionEvent简介 当用户触摸屏幕时,将创建一个MontionEvent对象,MotionEvent包含了关于发生触摸的位置.时间信息,以及触摸事件的其他细节. 获取MontionEvent对 ...

  4. 触摸事件MultiTouch Events

    备注: userInteractionEnabled = NO hidden = YES alpha = 0.0~0.01    //如果上面三个属性被设置了则无法接收触摸事件 1.- (void)t ...

  5. js 触摸事件 touch

    //ban 为某div let startX = 0; ban.addEventListener("touchstart",function(){ //获取初始点击位置 start ...

  6. IOS 触摸事件分发机制详解

    欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 作者:MelonTeam 前言 很多时候大家都不关心IOS触摸事件的分发机制的实现原理,当遇到以下几种情形的时候你很可能抓破头皮都找不到解决方案 ...

  7. HTML5触摸事件(touchstart、touchmove和touchend) (转)

    HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事件主 ...

  8. HTML5实战与剖析之触摸事件(touchstart、touchmove和touchend)(转)

    HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事件主 ...

  9. HTML5实战与剖析之触摸事件(touchstart、touchmove和touchend)

    HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事件主 ...

随机推荐

  1. 汤森路透为何一定要卖掉SCI?

    http://health.sohu.com/20160714/n459331727.shtml

  2. MySQL检查重复索引工具-pt-duplicate-key-checker

    在MySQL中是允许在同一个列上创建多个索引的,示例如下: mysql --socket=/tmp/mysql5173.sock -uroot -p mysql> SELECT VERSION( ...

  3. PHP 获取中国时间,即上海时区时间

    /** * 获取中国时间,即上海时区时间 * @param <type> $format * @return <type> */ function getChinaTime($ ...

  4. ThinkPHP v3.2.3 数据库读写分离,开启事务时报错:There is no active transaction

    如题:ThinkPHP v3.2.3 数据库读写分离,开启事务时报错: ERR: There is no active transaction 刚开始以为是数据表引擎不对造成的,因为 有几张表的引擎是 ...

  5. 【Alpha版本】冲刺总结随笔

    项目预期计划 确定代码规范与编码原则. 根据原型设计,界面设计,搭建应用大致框架,完善控件,背景等的界面设计. 根据体系结构设计,完善界面跳转逻辑,确定功能模块,实现1.0版本功能. 重点完善需求说明 ...

  6. Alpha版本冲刺总结——曙光初现

    No Bug 031402401鲍亮 031402402曹鑫杰 031402403常松 031402412林淋 031402418汪培侨 031402426许秋鑫 项目预期计划 界面设计 androi ...

  7. thinkphp一对多HAS_MANY

    关联关系通常我们所说的关联关系包括下面三种:一对一关联 :ONE_TO_ONE,包括HAS_ONE 和 BELONGS_TO 一对多关联 :ONE_TO_MANY,包括HAS_MANY 和 BELON ...

  8. java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead

    java.lang.IllegalStateException: Not allowed to create transaction on sharedEntityManager - use Spri ...

  9. js获取网站项目根路径

    //js获取项目根路径,如: http://localhost:8083/uimcardprj function getRootPath(){ //获取当前网址,如: http://localhost ...

  10. HDU 3879 Base Station(最大权闭合子图)

    经典例题,好像说可以转化成maxflow(n,n+m),暂时只可以勉强理解maxflow(n+m,n+m)的做法. 题意:输入n个点,m条边的无向图.点权为负,边权为正,点权为代价,边权为获益,输出最 ...