WHAT'S NEW?
Loading...

Motion Tween with line As3


/*
Line 7-32
Original script by GMaker0507, http://www.actionscript.org/forums/member.php3?u=68019
from: http://www.actionscript.org/forums/showthread.php3?t=174057
*/

var Waypoints:Array = [];
var moveSpeed:Number= 3;
var lineFlag:Boolean;

function AddWaypoint (xPos:Number,yPos:Number) {
    Waypoints.push (new Point(xPos,yPos));
}

function MoveToWaypoint () {
    if (Waypoints.length > 0) 
 {
        var difference:Point=Waypoints[0].subtract(new Point(box.x,box.y));
        var distance = difference.length;
  trace(distance);
        if ((distance-moveSpeed) <= 1) {
            box.x = Waypoints[0].x;
            box.y = Waypoints[0].y;
            Waypoints.splice (0,1);
        } else {
   difference.normalize (1);
            box.x+=difference.x*moveSpeed;
            box.y+=difference.y*moveSpeed;
   trace(difference.x);
        }
    } else removeEventListener(Event.ENTER_FRAME,EnterFrame);
}

graphics.lineStyle(2,0xff0000);
stage.addEventListener(MouseEvent.MOUSE_DOWN, stageDownHandler);

function stageDownHandler(e:MouseEvent):void
{
 stage.addEventListener(MouseEvent.MOUSE_MOVE, stageMoveHandler);
 stage.addEventListener(MouseEvent.MOUSE_UP, stageUpHandler);
}

function stageUpHandler(e:MouseEvent):void
{
 stage.removeEventListener(MouseEvent.MOUSE_MOVE, stageMoveHandler);
 stage.removeEventListener(MouseEvent.MOUSE_UP, stageUpHandler);
}

function stageMoveHandler(e:MouseEvent):void
{
 trace(stage.mouseX, stage.mouseY);
 AddWaypoint(stage.mouseX, stage.mouseY);
 if(!lineFlag)
 {
  graphics.moveTo(stage.mouseX, stage.mouseY);
  lineFlag=true;
 }
 graphics.lineTo(stage.mouseX,stage.mouseY);
}
/*stage.addEventListener(MouseEvent.CLICK, stageClickHandler);
function stageClickHandler(e:MouseEvent):void
{
 AddWaypoint(stage.mouseX, stage.mouseY);
 if(!lineFlag)
 {
  graphics.moveTo(stage.mouseX, stage.mouseY);
  lineFlag=true;
 }
 graphics.lineTo(stage.mouseX,stage.mouseY);
}
*/
function EnterFrame(event:Event) 
{
 MoveToWaypoint()
}

_start.addEventListener(MouseEvent.CLICK, startClickHandler);

function startClickHandler(e:MouseEvent):void
{
/* stage.removeEventListener(MouseEvent.CLICK, stageClickHandler);
*/ addEventListener(Event.ENTER_FRAME,EnterFrame);
}