我正在adobe air开发一款游戏,其中我在中心有一个电影剪辑。当用户触摸此影片剪辑并在舞台上移动时,影片剪辑需要相应地将其“x”更改为触摸的位置。我正在使用以下代码,它完全按照预期的方式执行:
MC.addEventListener(TouchEvent.TOUCH_MOVE, touchDownMC);
function touchDownMC(e:TouchEvent):void {
MC.x = e.stageX;
}
MC正在移动,但问题是,在用户按下MC并移动它10-15秒后,MC只是停止响应当前触摸事件,并且用户需要重新触摸它并向后移动。好像它正在失去TouchEvent。只要我的手指放在上面,我怎样才能使MC不断移动,让它保持聆听和移动?
答案 0 :(得分:3)
找到了解决方案!最好的方法是使用我在SO AS3/AIR check if TouchPhase.ENDED is over object
上找到的代码这是我在我的案例中实现它的方式:
MC.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
function onTouchBegin(event:TouchEvent) {
if(touchMoveID != 0) {
// myTextField.text = "already moving. ignoring new touch";
trace("It Did Not");
return;
}
touchMoveID = event.touchPointID;
// myTextField.text = "touch begin" + event.touchPointID;
stage.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
stage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);
}
function onTouchMove(event:TouchEvent) {
if(event.touchPointID != touchMoveID) {
// myTextField.text = "ignoring unrelated touch";
return;
}
MC.x = event.stageX;
MC.y = event.stageY;
// myTextField.text = "touch move" + event.touchPointID;
}
function onTouchEnd(event:TouchEvent) {
if(event.touchPointID != touchMoveID) {
// myTextField.text = "ignoring unrelated touch end";
return;
}
touchMoveID = 0;
stage.removeEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
stage.removeEventListener(TouchEvent.TOUCH_END, onTouchEnd);
// myTextField.text = "touch end" + event.touchPointID;
}