Draw (Click and Drag) to animate the red ball…3

Vodpod videos no longer available.

more about "animate_along_path", posted with vodpod

AS3:

stage.frameRate = 30;
var pencil:Shape = new Shape();
addChild(pencil);

var index:int = 0;
var points:Array = new Array();
var circle:Shape = new Shape();
circle.graphics.beginFill(0xff6600);
circle.graphics.drawCircle(0,0, 5);
addChild(circle);
circle.x = stage.stageWidth/2;
circle.y = stage.stageHeight/2;
clear_btn.buttonMode = true;

clear_btn.addEventListener(MouseEvent.MOUSE_DOWN, clearStage);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
circle.addEventListener(Event.ENTER_FRAME, onMoveCircle);

function clearStage(evt:MouseEvent):void {
addChild(circle);
}

function onDown(evt:MouseEvent):void {
pencil.graphics.lineStyle(0,0×999999);
pencil.graphics.moveTo(mouseX, mouseY);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onDraw);
}

function onUp(evt:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onDraw);
}

function onDraw(evt:Event):void {
pencil.graphics.lineTo(mouseX, mouseY);
points.push(new Point(mouseX, mouseY));
}

function onMoveCircle(evt:Event):void {
if (points.length>0) {
circle.x += (points[index].x – circle.x) / 4;
circle.y += (points[index].y – circle.y) / 4;
index++;
if (index==points.length) {
index=0;
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s