Skip to main content
Known Participant
April 27, 2016
Question

Action Script3 convert mouseEvents to touchEvents

  • April 27, 2016
  • 1 reply
  • 929 views

Hi all, I have been following a tutorial on drag and drop using mouse buttons. I need to do this using touch gestures Touch and Drag.

I have the code to do this already but now either I need to start again or convert the code I already have.

I have  movieClips as shapes and shape targets.  The idea is for the correct moviclip to be dragged to the correct targets. If the movieclip hits the correct target it stops and if the wrong target it returns to its starting position. I know I can use hit test point to confirm the moviclips have gone to correct targets.

Like I say i have the code to do this with mouse clicks but not sure where to start for mobile devices. I'm new to coding and thought it would be simple to change from mouse to gestures but I don't think I have enough experience.

If some wise member could help me to connect this up I would be most grateful.

I have been using the function below. As far as I can tell the MovieClip is stored in a variable(item) when it is clicked on. Then when its released it either snaps to the reg point of the target or returns to its original positon.

item1_mc.addEventListener(MouseEvent.MOUSE_UP, item1Release);

item1_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);

function dragTheObject(event:MouseEvent):void {

    var item:MovieClip=MovieClip(event.target);

    item.startDrag();

    var topPos:uint=this.numChildren-1;

    this.setChildIndex(item, topPos);

}

function item1Release(event:MouseEvent):void {

    var item:MovieClip=MovieClip(event.target);

    item.stopDrag();

    if (target1_mc.hitTestPoint(item.x,item.y)) {

        item.x=target1_mc.x;

        item.y=target1_mc.y;

    } else {

       item.x=orig1X;

       item.y=orig1Y;

    }

};

How to convert this for the gesture touch and drag?

Edit I have also posted this in Adobe Animate forum. Hope this is ok.

    This topic has been closed for replies.

    1 reply

    sinious
    Legend
    April 27, 2016

    The main difference is a mouse is singular while a touch events fire off for multiple concurrent touch positions. You can work either in single touch mode, similar to a mouse, or handle all touch points.

    Have you looked into the Adobe examples of handling touch events? Here's a simple Adobe code example of touch/drag:

    Adobe Flash Platform * Touch and drag

    Known Participant
    April 27, 2016

    Thanks  sinious, I will check that out.

    sinious
    Legend
    May 2, 2016

    You're welcome and feel free to post any trouble code converting over to touch events and we can help.