问题
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
?
the code :
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
}
}
}
回答1:
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 );
}
}
}
Here , currentTarget
is Stage
, and target
will be your image
来源:https://stackoverflow.com/questions/16414878/add-mouseevent-click-to-starling-image