将流体触摸事件分配给影片剪辑AS3

时间:2013-05-27 22:05:12

标签: actionscript-3 touch-event

嘿,大家好,我有一个简短的问题。我有一个响应用户触摸的电影剪辑。它是一艘向上或向下线性移动的船。现在触摸事件工作正常我通过在舞台上创建2个单独的电影剪辑来设置它们,这些剪辑充当控制船的按钮。因此,当用户触摸顶部按钮时,船向上移动同样适用于底部按钮。这款游戏适用于Android手机。所以我的问题是如何将它带到我不需要任何影片剪辑按钮来控制船的位置?当用户向上或向下移动手指时,我无法将其发送到触摸事件响应的位置。而不是必须从手机上取下手指,然后将其放在一个单独的按钮上。我已经在许多其他游戏中看到过这种情况,如果你在手指仍然在手机上时上下移动它,它会响应它并向正确的方向移动。这就是我现在在我的构造函数中设置我的按钮和触摸事件的方法:

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

我有这些功能:

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

这就是现在控制船只的东西。如上所述,它工作正常。只是想看看他们是否有任何方法可以摆脱按钮,只要手指改变方向就让它在向上或向下位置做出反应。任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:1)

不要在对象上添加mouseUp / TouchOut侦听器,而是在mouseDown / TouchOver处理程序内的舞台上侦听它们。像这样:

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.
      }
}

通过触摸(如果支持多点触控),您还需要跟踪touchPointID TouchEvent,否则即使您只松开一根手指,touchOut也会触发所有触摸点。< / p>