我有2个圈子,都有触控听众。 有时候一个圆圈在舞台上与另一个圆圈重叠,我想要触发两个听众,但是椋鸟只触发前面的圆圈。
我该怎么办?
我的代码
circle1.addEventListener(TouchEvent.TOUCH, touched1)
circle2.addEventListener(TouchEvent.TOUCH, touched2)
function touched1(e:TouchEvent):void{
trace("hi1")
}
function touched2(e:TouchEvent):void{
trace("hi2")
}
答案 0 :(得分: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);
}
答案 1 :(得分: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();
}
}