在as3中用多点触摸同时拖动两个对象 [英] simultaneous drag two objects with multitouch in as3

查看:438
本文介绍了在as3中用多点触摸同时拖动两个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在AS3中使用多点触摸同时拖动两个对象.我的目标是让用户将两个对象捏在一起.现在,我不能让两个人同时移动.有任何想法为什么这行不通吗?

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

推荐答案

这是最简单,最直接的方法来完成您要问的事情:

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

这篇关于在as3中用多点触摸同时拖动两个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆