0
votes

Hey guys so i have a quick question. I have a Movie Clip which responds to the users touch. Its a ship that moves up or down linear. Now the touch events work fine i set them up by creating 2 separate Movie clips on the stage which act as buttons to control the ship. So when the user touches the top button the ship moves up same goes for the bottom button. This game is for android mobile by the way. So my question is how would i make it to where i dont need any movie clip buttons to control the ship? Couldnt i make it to where the touch events respond to when the user moves his finger up or down. Instead of having to take my finger off the phone and then placing it on a separate button. Ive seen this done on many other games where if you move it up or down while your finger is still on the phone it responds to it and moves in the correct direction. This is how i have my buttons and touch events set up right now, in my constructor function:

//setup Listeners when btn's are rolled over and out
        btnUp.addEventListener(TouchEvent.TOUCH_OVER, btnUpMouseOver, false, 0, true); 
        btnUp.addEventListener(TouchEvent.TOUCH_OUT, btnUpMouseOver, false, 0, true);
        btnDown.addEventListener(TouchEvent.TOUCH_OVER, btnDownMouseOver, false, 0, true); 
        btnDown.addEventListener(TouchEvent.TOUCH_OUT, btnDownMouseOver, false, 0, true);
        shootPlayer.addEventListener(TouchEvent.TOUCH_TAP, shootPlayerObject, false, 0, true);

THen i have the functions:

private function btnDownMouseOver(e:TouchEvent):void 
    {
        //trace("btnDownRollover");
        if (e.type == TouchEvent.TOUCH_OVER)
          {
              //Mouse was pressed on this button
             btnPressedDown = true;

          }
          else
          {
              //Mouse was released
              btnPressedDown = false;
          }
    }

    private function btnUpMouseOver(e:TouchEvent):void 
    {
        //trace("btnUpRollOver");
          if (e.type == TouchEvent.TOUCH_OVER)
          {
              //Mouse was pressed on this button
              btnPressedUp = true;



          }
          else
          {
              //Mouse was released
              btnPressedUp = false;
          }
    }

so thats what controls the ship as of now. It works fine as i stated above. Just wanted to see if their is any way to get rid of the buttons and just make it respond in either the up or down position whenever the finger changes direction. Any help will be appreciated thank you!

1

1 Answers

1
votes

Instead of adding your mouseUp/TouchOut listeners on the object, listen for them on the stage inside the mouseDown/TouchOver handlers. Like so:

private function btnDownMouseOver(e:TouchEvent):void 
{
    //you may need to do something with e.touchPointID if using multi-touch

    //Mouse was pressed on this button
    btnPressedDown = true;
    stage.addEventListener(TouchEvent.TOUCH_OUT, btnOut); //now that the mouse is down, lets listen for the up event globally

}

private function btnOut(e:TouchEvent):void
      {
          //Mouse was released
          btnPressedDown = false;
          stage.removeEventListener(TouchEvent.TOUCH_OUT,btnOut); //now the up event has fired, let's stop listening for it globally.
      }
}

With touch though (if supporting multitouch), you'll also need to track the touchPointID of TouchEvent, otherwise your touchOut will fire for all touch points even if you only released one finger.