add MouseEvent.CLICK to starling image

笑着哭i 提交于 2019-12-11 04:01:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!