starling教程-触摸事件(Touch Events)

在前面提到过,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属性:

private function onTouch (e:TouchEvent):void 
 { 
   // get the mouse location related to the stage 
     var touch:Touch = e.getTouch(stage); 
     var pos:Point = touch.getLocation(stage); 
  
     trace ( touch.phase ); 
  
     // store the mouse coordinates 
     _mouseY = pos.y; 
     _mouseX = pos.x; 
 }

当我们开始和一个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类的其他一些常用的属性:

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

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

 

让我们来看看Touch类的pai:

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

 

模拟多点触摸

当进行移动设备的开发时,你会有很多时候想要使用多点触摸的交互操作,比如放大缩小图片。当你再台式电脑上进行开发时,手里没有移动设备,这时你就会需要使用到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(); 
        } 
    } 
}


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

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

package  
 { 
     import flash.geom.Point; 
  
     import starling.display.Sprite; 
     import starling.events.Event; 
     import starling.events.Touch; 
     import starling.events.TouchEvent; 
     import starling.events.TouchPhase; 
  
     public class Game extends Sprite 
     { 
     private var customSprite:CustomSprite; 
      
         public function Game() 
         {   
       addEventListener(Event.ADDED_TO_STAGE, onAdded); 
         } 
  
     private function onAdded ( e:Event ):void 
     { 
       // create the custom sprite 
       customSprite = new CustomSprite(200, 200); 
        
       // positions it by default in the center of the stage 
       // we add half width because of the registration point of the custom sprite (middle) 
       customSprite.x = (stage.stageWidth - customSprite.width >> 1 ) + (customSprite.width >> 1); 
       customSprite.y = (stage.stageHeight - customSprite.height >> 1) + (customSprite.height >> 1); 
        
       // show it 
       addChild(customSprite); 
  
       // we listen to the mouse movement on the stage 
       //stage.addEventListener(TouchEvent.TOUCH, onTouch); 
       // need to comment this one ? ;) 
       stage.addEventListener(Event.ENTER_FRAME, onFrame); 
       // when the sprite is touched 
       customSprite.addEventListener(TouchEvent.TOUCH, onTouchedSprite); 
     } 
  
     private function onFrame (e:Event):void 
     { 
       // we update our custom sprite 
       customSprite.update(); 
     } 
  
     private function onTouchedSprite(e:TouchEvent):void 
     { 
             // retrieves the touch points 
             var touches:Vector.<Touch> = e.touches; 
  
             // if two fingers 
             if ( touches.length == 2 ) 
             { 
                 var finger1:Touch = touches[0]; 
                 var finger2:Touch = touches[1]; 
  
                 var distance:int; 
                 var dx:int; 
                 var dy:int; 
  
                 // if both fingers moving (dragging) 
                 if ( finger1.phase == TouchPhase.MOVED && finger2.phase == TouchPhase.MOVED ) 
                 { 
                     // calculate the distance between each axes 
                     dx = Math.abs ( finger1.globalX - finger2.globalX ); 
                     dy = Math.abs ( finger1.globalY - finger2.globalY ); 
                      
                     // calculate the distance 
                     distance = Math.sqrt(dx*dx+dy*dy); 
  
                     trace ( distance ); 
                 } 
             } 
     } 
     } 
 }


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值