1
votes

I am developing a game in adobe air, in which i have a movie clip at the center. When the user touches this movie clip and moves around the stage, the movie clips needs to change its "x" accordingly to the touch's position. I am using the following code, which is doing exactly what it intended to do :

    MC.addEventListener(TouchEvent.TOUCH_MOVE, touchDownMC);
function touchDownMC(e:TouchEvent):void {
    MC.x = e.stageX;
}

The MC is moving correctly, but the problem is, after 10-15 seconds while the user is pressing the MC and moving it around, the MC just stops responding for the current touch event, and the user needs to re-touch it and move back around. It seems like it's loosing the TouchEvent. How can i make the MC constantly move as long as i have my finger on it, keep it listening and moving?

1

1 Answers

3
votes

Found The Solution ! The best way to do so is to use this code that i found on SO AS3/AIR check if TouchPhase.ENDED is over object

This is how i implemented it in my case :

MC.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
function onTouchBegin(event:TouchEvent) { 
    if(touchMoveID != 0) { 
   //     myTextField.text = "already moving. ignoring new touch";     
     trace("It Did Not");
     return; 
    } 
    touchMoveID = event.touchPointID; 

 //   myTextField.text = "touch begin" + event.touchPointID; 
    stage.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove); 
    stage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd); 
} 
function onTouchMove(event:TouchEvent) { 
    if(event.touchPointID != touchMoveID) { 
     //   myTextField.text = "ignoring unrelated touch"; 
        return; 
    } 
    MC.x = event.stageX; 
    MC.y = event.stageY; 
 //   myTextField.text = "touch move" + event.touchPointID; 
} 
function onTouchEnd(event:TouchEvent) { 
    if(event.touchPointID != touchMoveID) { 
   //     myTextField.text = "ignoring unrelated touch end"; 
        return; 
    } 
    touchMoveID = 0; 
    stage.removeEventListener(TouchEvent.TOUCH_MOVE, onTouchMove); 
    stage.removeEventListener(TouchEvent.TOUCH_END, onTouchEnd); 
   // myTextField.text = "touch end" + event.touchPointID; 
}