在前面提到過,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)的講解。