0

嘿伙计们,所以我有一个快速的问题。我有一个响应用户触摸的影片剪辑。它是一艘可以线性向上或向下移动的船。现在触摸事件工作正常,我通过在舞台上创建 2 个单独的电影剪辑来设置它们,这些剪辑充当控制飞船的按钮。因此,当用户触摸顶部按钮时,船会向上移动,底部按钮也是如此。顺便说一下,这款游戏适用于安卓手机。所以我的问题是我如何才能到达不需要任何电影剪辑按钮来控制船的地方?当用户向上或向下移动手指时,我无法到达触摸事件响应的位置。无需将手指从手机上移开,然后将其放在单独的按钮上。我已经在许多其他游戏中看到过这种情况,如果您在手指仍在手机上时向上或向下移动它,它会响应它并朝正确的方向移动。

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

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

4

1 回答 1

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

虽然使用触摸(如果支持多点触摸),您还需要跟踪touchPointIDof TouchEvent,否则即使您只松开一根手指,您的 touchOut 也会针对所有触摸点触发。

于 2013-05-28T00:06:58.980 回答