這篇來談談Starling的Multi-Touch。前一篇也提到,Multi-Touch一樣是監聽TouchEvent.TOUCH,然後由TouchEvent的e.getTouches()取回多點的資訊。通常提到Multi-Touch會想到Gesture,不過Starling目前沒有GestureEvent可用。需要的時候只能自己動手寫。

在開始練習之前,我們要回到Document Class,Main.as。要使用Multi-Touch,要先設定Starling Class的靜態屬性:

Starling.multitouchEnabled = true; 

然後Starling也提供了可以在電腦上用滑鼠模擬Multi-Touch的功能,這個功能則要在我們產生Starling實體之後,設定:

_starling.simulateMultitouch = true; 

設定好之後,我們來做個Multi-Touch,以及自訂Gesture的功能。我們在場景上放一個Image,然後為這個Image加上兩指縮放、旋轉、平移等功能。先把程式碼列出來:

publicclassGame5extendsSprite
{
privatevar _container:Sprite;
privatevar _image:Image;
privatevar _msgText:TextField;
[Embed(source ="/assets/head.png")]//embed一張圖給Image當Texture用
privatestaticconstHeadBitmap:Class;

publicfunctionGame5()
{
super();
addEventListener(Event.ADDED_TO_STAGE, init);
}

privatefunction init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);

_container =newSprite();
addChild(_container);
addChild(newStats());

_image =newImage(Texture.fromBitmap(newHeadBitmap()));//新增一個Image,貼上Texture
_image.x =(stage.stageWidth - _image.width)>>1;//設定_image座標
_image.y =(stage.stageHeight - _image.height)>>1;//設定_image座標
_container.addChild(_image);//將_image加到場景上

_msgText =newTextField(300,20,"","Arial",14,0xFF0000,true);//新增一個Texture來顯示訊息
_msgText.x =100;
_msgText.y =80;
_msgText.hAlign =HAlign.LEFT;
_container.addChild(_msgText);

_image.addEventListener(TouchEvent.TOUCH, onTouch);//_image加上監聽TouchEvent.TOUCH
}

privatefunction onTouch(e:TouchEvent):void
{
var touches:Vector.<Touch>= e.getTouches(this);//取出多個touches的資訊
var target:Image=Image(e.target);//取得發出事件的target,在這裡只有_image

if(touches.length ==1)//如果只有一點touch
{
var touch:Touch= touches[0];//取得唯一一點touch
if(touch.phase ==TouchPhase.MOVED)//當這點在移動時
{
var currentPos:Point= touch.getLocation(target.parent);//取得這個touch的現在座標
var previousPos:Point= touch.getPreviousLocation(target.parent);//取得這個touch的上一次座標
target.x += currentPos.x - previousPos.x;//_image的x移動量為現在x座標減上次x座標
target.y += currentPos.y - previousPos.y;//_image的y移動量為現在y座標減上次y座標
}
}

if(touches.length ==2)//當touch有兩點時
{
var touchA:Touch= touches[0];//touchA為第一點
var touchB:Touch= touches[1];//touchB為第二點

var currentPosA:Point= touchA.getLocation(target.parent);//touchA現在座標
var previousPosA:Point= touchA.getPreviousLocation(target.parent);//touchA上次座標
var currentPosB:Point= touchB.getLocation(target.parent);//touchB現在座標
var previousPosB:Point= touchB.getPreviousLocation(target.parent);//touchB上次座標

var currentVector:Point= currentPosA.subtract(currentPosB);//現在兩點間的向量
var previousVector:Point= previousPosA.subtract(previousPosB);//上一次兩點間的向量

var currentAngle:Number=Math.atan2(currentVector.y, currentVector.x);//由現在向量算出現在弧度
var previousAngle:Number=Math.atan2(previousVector.y, previousVector.x);//算出上一次弧度
var deltaAngle:Number= currentAngle-previousAngle;//算出弧度差

//以上一次兩點座標更新pivot
var previousLocalA:Point= touchA.getPreviousLocation(target);//touchA的上一次座標(target自己的座標系)
var previousLocalB:Point= touchB.getPreviousLocation(target);//touchB的上一次座標(target自己的座標系)
target.pivotX =(previousLocalA.x + previousLocalB.x)*0.5;//以上次兩點中心為新的pivot
target.pivotY =(previousLocalA.y + previousLocalB.y)*0.5;//以上次兩點中心為新的pivot

//以現在兩點座標更新target座標
target.x =(currentPosA.x + currentPosB.x)*0.5;//以現在兩點算出新座標
target.y =(currentPosA.y + currentPosB.y)*0.5;//以現在兩點算出新座標

//旋轉
target.rotation += deltaAngle;//加上剛剛算出的旋轉量

//縮放
var sizeDiff:Number= currentVector.length / previousVector.length;//以前後兩的的向量長度比例算出縮放倍數
target.scaleX *= sizeDiff;//乘上縮放倍數
target.scaleY *= sizeDiff;//乘上縮放倍數
}

_msgText.text ="Touches number: "+ touches.length;//顯示現在touch點數
}
}

我們可以以touch.getLocation()以及touch.getPreviousLocation()來取得這次與上次的座標,輸入的參數是用來當作標基準的DisplayObject。當只有單一一點touch,而且在移動時,則平移target(_image)。當兩點時,使用兩點的前後座標,算出新的pivot中心、旋轉、縮放、新座標等。詳細的算法請參考程式碼與註解。要注意算pivot時我們是以target(_image)當座標系,因為pivot本來就是以target座標系來算;其他部分則以target.parent為座標系,我們一般得到某個DisplayObject的x與y座標也是以那個DisplayObject的parent座標系來算。

要使用模擬Multi-Touch,按住Ctrl可以看到兩個圓圈圖示,就代表兩個touch,移動滑鼠可以讓兩點往反方向移動。Ctrl+Shift可以讓兩點一起移動。Demo如下:

點我或圖看Demo

一起來玩鳥 Starling Framework(5)Multi-Touch的更多相关文章

  1. 一起來玩鳥 Starling Framework 簡介

    開場 Starling Framework是一套Flash 2D遊戲開發"工具",是使用Flash最新的Stage3D API建構出來的一套Framework.最大優點在於使用GP ...

  2. 一起來玩鳥 Starling Framework(4)TouchEvent,Touch,以及TouchPhase

    這一篇來介紹一下TouchEvent.我們先來談單點的touch,下一篇再介紹MultiTouch.翻過Starling文件的應該會發現,Starling裡面沒有MouseEvent,而是整合在Tou ...

  3. 一起來玩鳥 Starling Framework(2)效能測試以及Image與Texture

    上一篇我們放了一個Quad與TextField在舞台上慢慢轉.眼尖的可能會發現轉起來邊緣有點鋸齒,這可以透過設定Starling的反鋸齒來解決,在Main.as裡,新增了_starling之後,可以加 ...

  4. 一起來玩鳥 Starling Framework(1)一定要的Hello World!

    雖然已經一堆Hello World的介紹文章跟影片了,但中文資料畢竟是比較少,所以不能免俗的來一篇中文版Hello World.首先開啟一個AS3.0專案,fps不用客氣,設為60,Starling很 ...

  5. 一起來玩鳥 Starling Framework(7)MovieClip

    承上一篇,我們接著來講最後一個IAnimatable類別,MovieClip.Starling的MovieClip跟native的MovieClip不太一樣,它只能接收一個Vector.<Tex ...

  6. 一起來玩鳥 Starling Framework(9)Particle

    最後,來看看Starling裡一個很炫的功能:Particle.Particle屬於extension,所以要另外下載檔案:Starling-Extension-Particle-System.下載之 ...

  7. 一起來玩鳥 Starling Framework(6)Juggler、Tween、以及DelayCall

    這篇開始來講Starling裡的Animation.Juggle是個簡單的Class,用來控制動畫的進行.他負責管理經由add()加進來的實現IAnimatable介面的物件,然後當Juggler的a ...

  8. 一起來玩鳥 Starling Framework(3)Button!

    週末夜來介紹個簡單的DisplayObject就好.不論是在電腦上或行動裝置上,跟使用者互動次數最多的,大概就是按鈕了.因此,Starling有個Button類別,來做出這個常用的互動元件. 同樣是因 ...

  9. 一起來玩鳥 Starling Framework(8)BitmapFont

    所謂BitmapFont,就是事先將我們會用到的字型,會用到的字,輸出成一張圖片,類似Sprite sheet,以及一個xml格式的Data file,然後我們一次將這文字圖片轉成Texture,up ...

随机推荐

  1. linux察看安装包有那些

    rpm -ql  zsh |more  安装完之后,产生那些内容 which zsh rpm -qpl /mnt/packages/ rpm -Uvh /mnt/packages/lrzsz-0.2. ...

  2. 给tomcat单独配置jdk

    在catalina 文件 加这句话,前面加 export JAVA_HOME=/home/apache-tomcat-8.5.8/jdk1.8.0_101

  3. snakes

    原地址 讨论区 Changing 算法一 我会随机! 由于我忘了设置多组数据,期望得分0至100. 算法二 我会模拟! 复杂度\(O(t^2)\),期望得分60. 但是很多人忘记题目给出的是环形-- ...

  4. hdu 1140(三维)

    War on Weather Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  5. Win10系统解决C盘分区限制一半的问题

    1,按照网上的方法还不行,如链接 2,安装如下软件,里面有激活码,链接 链接:https://pan.baidu.com/s/14ifYpnCMGwJIbgykTYQR6Q 密码:whh3 3,安装并 ...

  6. django CXRF介绍

    CSRF(Cross-site request forgery)跨站请求伪造,是攻击者利用用户的身份操作用户帐户的一种攻击方式.和XSS攻击一样,存在巨大的危害性. 一.攻击方法 1.低级的CXRF攻 ...

  7. 创建展开行明细编辑表单的 CRUD 应用

    http://www.runoob.com/jeasyui/jeasyui-app-crud3.html jQuery EasyUI 应用 - 创建展开行明细编辑表单的 CRUD 应用 当切换数据网格 ...

  8. 某道我xjb想的题

    Function 时限:5s 空限:256M (都是单点) Discription 现在你有一个函数: inline int f(int x){ int tot=0,alr=0,now; while( ...

  9. 【动态规划】Gym - 100507G - The Debut Album

    一般思路的dp是用f(i,j,0)表示前i位最后有j个1的方案数,用f(i,j,1)表示前j位最后有j个2的方案数,j都是大于等于1的,然后比较容易转移. 但这题卡内存,就只能用f(i,j)表示前i位 ...

  10. 【bzoj1604】【[Usaco2008 Open]Cow Neighborhoods】简单的谈谈曼哈顿距离

    (最近p站上不去要死了) Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个"群".每只奶牛在吃 ...