问题描述
我有一个容器有滚动许多图像,我添加 TouchEvent.TOUCH
作为事件监听器,而不是 MouseEvent.CLICK
,因为八哥不支持的MouseEvent
。问题是,当我浏览的图像将监听器,的TouchEvent
之间,而我并不需要这个?有没有解决办法,而不是 TouchEvenet
?
在code:
私有函数的onComplete(事件:对象类型:flash.events.Event):无效
{
位图数据=位图(的LoaderInfo(将event.target).content).bitmapData;
VAR newImage:图片= Image.fromBitmap(位图(的LoaderInfo(将event.target).content));
newImage.height = 154;
newImage.width = 180;
的addChild(newImage);
newImage.addEventListener(starling.events.TouchEvent.TOUCH,image_clickHandler);
}
私有函数image_clickHandler(事件:starling.events.TouchEvent):无效
{
VAR触摸:触摸= event.getTouch(阶段);
如果(碰!= NULL)
{
如果(touch.phase == TouchPhase.BEGAN)
{
//一些code
}
}
}
这是一个很好的做法,是不会把事件侦听器的每次在场景中的对象。你应该只附着一个监听到舞台,之后读触摸事件的目标
属性。
这里从Inroducing八哥的例子书
私有函数的onClick(E:的TouchEvent):无效
{
VAR接触:矢量<触摸和GT。 = e.getTouches(本);
VAR点击:的DisplayObject = e.currentTarget为的DisplayObject;
如果(touches.length == 1)
{
VAR触摸:触摸=触摸[0];
如果(touch.phase == TouchPhase.ENDED)
{
跟踪(e.currentTarget,e.target);
}
}
}
下面, currentTarget当前
是舞台
和目标
将你的形象
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
}
}
}
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
这篇关于添加MouseEvent.CLICK到八哥形象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!