将 MouseEvent.CLICK 添加到八哥图像 [英] add MouseEvent.CLICK to starling image
问题描述
我有一个容器有很多带有滚动的图像,我添加 TouchEvent.TOUCH
作为事件侦听器而不是 MouseEvent.CLICK
,因为 Starling 不支持 鼠标事件
.问题是当我在图像之间导航时,它会监听 TouchEvent
而我不需要这个?有什么解决方案可以代替 TouchEvenet
吗?
i have a container have many image with scroll , i add TouchEvent.TOUCH
as event listener instead of MouseEvent.CLICK
, because starling doesn't support MouseEvent
.
the problem is when i navigate between images it will listener to TouchEvent
while i don't need to this ? is there any solution instead of TouchEvenet
?
代码:
private function onComplete (event:flash.events.Event):void
{
bitmapData = Bitmap(LoaderInfo(event.target).content).bitmapData;
var newImage:Image = Image.fromBitmap(Bitmap(LoaderInfo(event.target).content));
newImage.height = 154;
newImage.width = 180;
addChild(newImage);
newImage.addEventListener(starling.events.TouchEvent.TOUCH,image_clickHandler);
}
private function image_clickHandler(event:starling.events.TouchEvent):void
{
var touch:Touch = event.getTouch(stage);
if (touch != null)
{
if (touch.phase == TouchPhase.BEGAN)
{
//some code
}
}
}
推荐答案
最好不要将事件侦听器附加到场景中的每个 对象.您应该只将 一个 侦听器附加到舞台上,然后读取触摸事件的 target
属性.
这是介绍 Starling"一书中的示例:
It's a good practice not to attach event listeners to every objects on scene . You should attach only one listener to stage and after that read touch event's target
property.
Here the example from "Inroducing Starling" book:
private function onClick(e:TouchEvent):void
{
var touches:Vector.<Touch> = e.getTouches(this);
var clicked:DisplayObject = e.currentTarget as DisplayObject;
if ( touches.length == 1 )
{
var touch:Touch = touches[0];
if ( touch.phase == TouchPhase.ENDED )
{
trace ( e.currentTarget, e.target );
}
}
}
这里,currentTarget
是 Stage
,target
将是你的形象
Here , currentTarget
is Stage
, and target
will be your image
这篇关于将 MouseEvent.CLICK 添加到八哥图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!