1

我试图让提示在 MOUSE_DOWN 事件中停止旋转,但继续将 mouseX 和 mouseY 的距离添加到提示与 cueball 的距离。换句话说,为了使球杆沿着线滑动,球杆的目标是向下。

    Cue.addEventListener(Event.ENTER_FRAME, aimCue);
    Cue.addEventListener(MouseEvent.MOUSE_DOWN,StartToShoot);
    addEventListener(Event.ENTER_FRAME,MoveBalls);

    public function aimCue(event:Event)
    {
        var dx:Number = Cueball.x - Cue.x;
        var dy:Number = Cueball.y - Cue.y;
        var angle:Number = Math.atan2(dy,dx);
        Cue.rotation = angle * 180 / Math.PI;
        Cue.x = mouseX;
        Cue.y = mouseY;
    }
    public function StartToShoot(event:MouseEvent)
    {
        var dx:Number = Cueball.x - mouseX;
        var dy:Number = Cueball.y - mouseY;
        var dist:Number = Math.sqrt(dx * dx + dy * dy);
        //Cue.x = dx + Cueball.x;
        //Cue.y = dy + Cueball.y;


        if (dist > 230)
        {
            Cue.startx = Cue.x;
            Cue.starty = Cue.y;

            Cue.addEventListener(Event.ENTER_FRAME,FinishShoot);
        }
    }
    public function FinishShoot(event:Event)
    {
        var dx:Number = Cueball.x - Cue.x;
        var dy:Number = Cueball.y - Cue.y;
        var dist:Number = Math.sqrt(dx * dx + dy * dy);

        if (dist < 230)
        {
            Cueball.vx = Cue.x - Cue.startx;
            Cueball.vy = Cue.y - Cue.starty;
            Cue.removeEventListener(Event.ENTER_FRAME,FinishShoot);
        }

    }
4

2 回答 2

1

例如,您可以添加一个布尔 isShooting。在您的 MOUSE_DOWN 处理程序中,您可以将其设置为 true,并且在 aimCue 中您可以检查它是否为真。完成拍摄后,您可以再次将其设置为 false。

Cue.addEventListener(Event.ENTER_FRAME, aimCue);
Cue.addEventListener(MouseEvent.MOUSE_DOWN,StartToShoot);
addEventListener(Event.ENTER_FRAME,MoveBalls);    

private var isShooting:Boolean = false;

public function aimCue(event:Event)
{
    if(!isShooting)
    {
        var dx:Number = Cueball.x - Cue.x;
        var dy:Number = Cueball.y - Cue.y;
        var angle:Number = Math.atan2(dy,dx);
        Cue.rotation = angle * 180 / Math.PI;
        Cue.x = mouseX;
        Cue.y = mouseY;
    }
}
public function StartToShoot(event:MouseEvent)
{

    isShooting = true;

    var dx:Number = Cueball.x - mouseX;
    var dy:Number = Cueball.y - mouseY;
    var dist:Number = Math.sqrt(dx * dx + dy * dy);
    //Cue.x = dx + Cueball.x;
    //Cue.y = dy + Cueball.y;


    if (dist > 230)
    {
        Cue.startx = Cue.x;
        Cue.starty = Cue.y;

        Cue.addEventListener(Event.ENTER_FRAME,FinishShoot);
    }
}

对于移动提示的逻辑,您可以使用以下内容:(完成拍摄后不要忘记移除舞台监听器)

private var storedPoint:Point;

public function StartToShoot(event:MouseEvent)
{

    isShooting = true;

    storedPoint = new Point(stage.mouseX, stage.mouseY);

    stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}

private function onMouseMove(e:MouseEvent):void
{
    var curPoint:Point = new Point(stage.mouseX, stage.mouseY);
    var distance:Number = Point.distance(storedPoint, curPoint);

    /* do stuff with the distance*/

    e.updateAfterEvent();
}
于 2011-11-17T07:53:25.987 回答
0

例如,您可以使用任何 Tweening 库,例如greensock.com

至少,您需要输入球杆目的地的 x 和 y 坐标,而 Tweening 库应该处理其余的...

假设您用鼠标“射击”,您可以使用鼠标向下坐标和鼠标向上坐标确定轴。

当你有一个轴时,你只需要通过检查轴和母球重叠的位置来计算一个母球击球点。这将为您提供球杆的 x、y 目标坐标。

于 2011-11-17T09:34:16.867 回答