【问题标题】:Flash AS3 hit test go to next frameFlash AS3 命中测试转到下一帧
【发布时间】:2013-02-04 18:25:14
【问题描述】:

我有一个可以拖入另一个对象的对象。我已经为碰撞设置了命中测试。当发生碰撞时,我想前进到下一帧,但是我必须单击可拖动对象才能这样做。我希望它无需单击即可立即移至下一帧。有没有办法解决这个问题?

我的意思是在我拖动对象以创建碰撞后,我需要再次单击对象以前进到下一帧。我不想再次单击对象,我希望它在发生碰撞时转到下一帧。

这是我的代码

bottle.buttonMode = true;

bottle.addEventListener(MouseEvent.MOUSE_DOWN, drag);

bottle.addEventListener(MouseEvent.MOUSE_UP, drop);


function collision():void{
   if(bottle.hitTestObject(hit)){
    nextFrame();
    }
  }

 function drag(e:MouseEvent):void{
 bottle.startDrag();
 collision();
}



    function drop(e:MouseEvent):void{
    bottle.stopDrag();
}

【问题讨论】:

  • 如果不点击移动物体,怎么碰撞?

标签: actionscript-3 flash hittest


【解决方案1】:

您应该在drop 之后检查冲突,而不是在drag 的开头:

function collision():void{
    if(bottle.hitTestObject(hit)){
        nextFrame();
    }
}

function drag(e:MouseEvent):void{
    bottle.startDrag();
}

function drop(e:MouseEvent):void{
    bottle.stopDrag();
    collision();
}

【讨论】:

    【解决方案2】:

    collision()更改为事件监听器并将其附加到bottle

    【讨论】:

      【解决方案3】:

      试试这个(改编自 Gary Rosenzweig):

      bottle.buttonMode = true;
      
      bottle.addEventListener( MouseEvent.MOUSE_DOWN, startBottleDrag );
      stage.addEventListener( MouseEvent.MOUSE_UP, stopBottleDrag );
      
      function collision():void {
          if( bottle.hitTestObject( hit ) ) {
              stopBottleDrag();
              nextFrame();
          }
      }
      
      // to keep bottle location as it is when clicked
      var clickOffset:Point = null;
      
      function startBottleDrag( e:MouseEvent ) {
          clickOffset = new Point( e.localX, e.localY );
          bottle.addEventListener( Event.ENTER_FRAME, dragBottle );
      }
      
      function stopBottleDrag( e:MouseEvent = null ) {
          clickOffset = null;
          bottle.removeEventListener( Event.ENTER_FRAME, dragBottle );
      }
      
      function dragBottle( e:Event ) {
          bottle.x = mouseX - clickOffset.x;
          bottle.y = mouseY - clickOffset.y;
          collision();
      }
      

      【讨论】:

        猜你喜欢
        • 2012-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多