使用as3中的多点触控同时拖动两个对象

时间:2014-11-25 21:24:20

标签: 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);

1 个答案:

答案 0 :(得分:2)

这是您提出要求的最简单,最直接的方式:

    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);
    }