actionscript-3 - 在as3中使用多点触控同时拖动两个对象

标签 actionscript-3 drag multi-touch

我试图在 AS3 中使用多点触控同时拖动两个对象。我的目标是让用户将两个对象捏在一起。现在我无法让两者同时移动。有什么想法为什么这不起作用吗?

Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

//

bullseye4a.addEventListener(TouchEvent.TOUCH_BEGIN, fl_ClickToDrag4a);

function fl_ClickToDrag4a(event: TouchEvent): void {
bullseye4a.startDrag();
}

bullseye4b.addEventListener(TouchEvent.TOUCH_BEGIN, fl_ClickToDrag4b);
function fl_ClickToDrag4b(event: TouchEvent): void {
bullseye4b.startDrag();
}

bullseye4a.addEventListener(TouchEvent.TOUCH_END, fl_ReleaseToDrop4a);
function fl_ReleaseToDrop4a(event: TouchEvent): void {
bullseye4a.stopDrag();

}
bullseye4b.addEventListener(TouchEvent.TOUCH_END, fl_ReleaseToDrop4b);
function fl_ReleaseToDrop4b(event: TouchEvent): void {
bullseye4b.stopDrag();
}
addChild(bullseye4a);
addChild(bullseye4b);

最佳答案

这是实现您所要求的最简单、最直接的方法:

    Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

    var curTouchPoints:Dictionary = new Dictionary(); //a dictionary to store which objects are related to which touch points


    bullseye4a.addEventListener(TouchEvent.TOUCH_BEGIN, touchStart); //add both objects touch begin listener
    bullseye4b.addEventListener(TouchEvent.TOUCH_BEGIN, touchStart);

    //add a global touch move listener
    stage.addEventListener(TouchEvent.TOUCH_MOVE, touchMove);




    function touchStart(e:TouchEvent):void {
        //create an object that stores the offset and a the object touched, then add it to the dictionary
        curTouchPoints[e.touchPointID] = {obj: e.currentTarget, offsetX: e.localX, offsetY: e.localY}; //store the current object in the dictionary

        //listen for the touch end event 
        e.currentTarget.addEventListener(TouchEvent.TOUCH_END,touchEnd);
    }

    function touchMove(e:TouchEvent):void {
        //move this object to the current touch position
        //find the object by looking up the touchPointId in the dictionary (since e.currentTarget will be the stage, and e.target could be the child of what you really want OR the stage if touch 'leaves' the object)
        DisplayObject(curTouchPoints[e.touchPointID].obj).x = e.stageX - curTouchPoints[e.touchPointID].offsetX;  //subtract the offset so the object doesn't snap to the registration point on the first touch move
        DisplayObject(curTouchPoints[e.touchPointID].obj).y = e.stageY - curTouchPoints[e.touchPointID].offsetY;
    }

    function touchEnd(e:TouchEvent):void {
        //remove the dictionary item now that the touch has ended
        delete curTouchPoints[e.touchPointID];

        //remove the touch end listener
        e.currentTarget.removeEventListener(TouchEvent.TOUCH_END,touchEnd);
    }

关于actionscript-3 - 在as3中使用多点触控同时拖动两个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27136960/

相关文章:

javascript - 无法在 html 文本字段中输入文本

swift - 在范围内找不到类型 'View'

java - Android MultiTouch 仅适用于一根手指

java - 具有多个 View 的 Android 触摸事件

javascript - 我的拖放功能不起作用。我收到类型错误。这是什么意思?

c++ - 在 Linux 上检测事件触摸屏(如果是多点触控)

apache-flex - 将 flashvars 样式的参数传递给加载的 SWF

actionscript-3 - 外部 SWF 加载问题

actionscript-3 - Flash ActionScript 文件突然无法编译?

flash - 在 FF 中,按键滞后于鼠标