Trying to find a way to "slide" cuestick in Actionscript 3.0 Billiards Game

155 views Asked by At

I am trying to get the cue to stop rotating when it is in the MOUSE_DOWN event, but continue to add the distance of mouseX and mouseY to the distance of the cue from the cueball. In other words, for the cue to slide along the line the cue is aimed down.

    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

There are 2 answers

2
Rick van Mook On BEST ANSWER

You could add a Boolean isShooting for instance. In your MOUSE_DOWN handler you can set it to true and within the aimCue you can check wether it's true or not. When your done shooting, you can set it to false again.

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

For the logic to move your cue you could use something like this: (don't forget to remove the stage listener when you're finished shooting)

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

You could use any Tweening library such as greensock.com for instance.

At the minimum you would need to input the x & y coordinates for the destination of the cue stick & the Tweening library should handle the rest...

Let's say that you "shoot" with the mouse, you can determine an axis using the mouse down coordinates & the mouse up coordinates.

When you have an axis , you simply need to work out a cue ball hit point by checking where the axis and the cue ball overlap. This will give you the x, y destination coordinates for the cue stick.