2014-02-25 29 views
0

我有2個圓圈,兩個都有觸摸監聽器。 有些時候,一個圓與另一個在舞臺上重疊,我想觸發兩個聽衆,但starling只觸發前面的圓圈。Starling - 在另一個觸摸事件下獲取

我該怎麼辦?

我的代碼

circle1.addEventListener(TouchEvent.TOUCH, touched1) 
circle2.addEventListener(TouchEvent.TOUCH, touched2) 
function touched1(e:TouchEvent):void{ 
    trace("hi1") 
} 
function touched2(e:TouchEvent):void{ 
    trace("hi2") 
} 

回答

1

代替具有兩個單獨的聽衆,有一個父對象只有一個監聽器。並在這個父對象中添加這兩個圓圈。爲了檢測該目標被觸摸然後,使用e.target.name像這樣:

var parentClip:Sprite = new Sprite(); 
parentClip.x = 150; // x position 
parentClip.y = 150; // y position 
parentClip.name = "parentClip"; //This is not required in your case 

parentClip.addChild(circle1); 
parentClip.addChild(circle2); 

parentClip.addEventListener(TouchEvent.TOUCH, onTouched); 

function onTouched(e:TouchEvent):void 
{ 
    trace("circles parent is touched"); 
    trace("Hit: " + e.currentTarget.name); 
    trace("Hit: " + e.target.name); 
} 
+0

MouseEvent in Starling? – esdebon

+0

糟糕?抱歉。在回答問題時我忘記了八哥。感謝您指點。 –

1

也許檢測所述圈處於接觸。試試這個:

circle1.addEventListener(TouchEvent.TOUCH, touched1) 
circle2.addEventListener(TouchEvent.TOUCH, touched2) 

function touched1(e:TouchEvent=null):void 
{ 
    trace("hi1"); 

    if (circle1.getBounds(circle1.parent).intersects(circle2.getBounds(circle2.parent)) == true) 
    { 
     touched2(); 
    } 
} 

function touched2(e:TouchEvent=null):void 
{ 
    trace("hi2") 

    if (circle2.getBounds(circle2.parent).intersects(circle1.getBounds(circle1.parent)) == true) 
    { 
     touched1(); 
    } 
}