topical media & game development
animation-ch08-Spring4.ax
animation-ch08-Spring4.ax
[swf]
[flash]
flex
package
{
import flash.display.Sprite;
import flash.events.Event;
public class @ax-animation-ch08-Spring4 extends Sprite
{
private var ball:animation_ch08_Ball;
private var spring:Number = 0.1;
private var targetX:Number = stage.stageWidth / 2;
private var targetY:Number = stage.stageHeight / 2;
private var vx:Number = 0;
private var vy:Number = 0;
private var friction:Number = 0.95;
public function @ax-animation-ch08-Spring4()
{
init();
}
private function init():void
{
ball = new animation_ch08_Ball();
addChild(ball);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
var dx:Number = mouseX - ball.x;
var dy:Number = mouseY - ball.y;
var ax:Number = dx * spring;
var ay:Number = dy * spring;
vx += ax;
vy += ay;
vx *= friction;
vy *= friction;
ball.x += vx;
ball.y += vy;
}
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.