简体   繁体   English

将MouseEvent.CLICK添加到Starling图片

[英]add MouseEvent.CLICK to starling image

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 . 我有一个带有滚动图像的容器,我将TouchEvent.TOUCH添加为事件侦听MouseEvent.CLICK ,而不是MouseEvent.CLICK ,因为TouchEvent.TOUCH不支持MouseEvent the problem is when i navigate between images it will listener to TouchEvent while i don't need to this ? 问题是当我在图像之间导航时,它将在TouchEvent时监听TouchEvent is there any solution instead of TouchEvenet ? 有什么解决方案可以代替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
            }   
        }

    }

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. 您应该仅将一个侦听器附加到舞台上,并且在该读取触摸事件的target属性之后。
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 在这里, currentTargetStagetarget将是您的图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM