1

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

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

这是我的代码

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

3 回答 3

0

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

于 2013-02-04T19:31:55.283 回答
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();
}
于 2013-02-05T00:19:21.980 回答
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();
}
于 2013-02-05T16:21:26.533 回答