问题
I am trying to drag two objects simultaneously with multitouch in AS3. My goal is to have the user pinch the two objects together. Right now I cannot get both to be moving at the same time. Any ideas why this isn't working?
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:
This is easiest and most direct way to do what you're asking:
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);
}
来源:https://stackoverflow.com/questions/27136960/simultaneous-drag-two-objects-with-multitouch-in-as3