Search code examples
flashactionscript-3billiards

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


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

    }

Solution

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