试图在Actionscript 3.0台球游戏中找到一种“滑动”cuestick的方法

时间:2011-11-17 06:33:34

标签: flash actionscript-3 billiards

我正试图让它在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);
        }

    }

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

至少你需要输入x&amp;提示杆的目的地的y坐标&amp;补间库应该处理剩下的......

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

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