Flash AS3命中测试转到下一帧

时间:2013-02-04 18:25:14

标签: actionscript-3 flash hittest

我有一个可以拖入另一个对象的对象。我已经为碰撞设置了一个命中测试。当发生碰撞时,我想进入下一帧但是,我必须单击可拖动对象才能执行此操作。我想立即移动到下一帧而不点击。无论如何要解决这个问题吗?

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

这是我的代码

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您应该在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 :(得分:0)

试试这个(改编自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();
}