MouseFollowerSmoother
MouseFollower.as
// forked from kadal's MouseFollower
// forked from kadal's Follower
// forked from kadal's Plasma
/* MouseFollower.as */
//
package {
import flash.display.Sprite;
import flash.display.Graphics;
import flash.display.DisplayObject;
import flash.display.BlendMode;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.filters.BlurFilter;
import net.hires.debug.Stats;
[SWF(backgroundColor="#000000", frameRate=30)]
public class MouseFollower extends Sprite {
private const SW:Number = stage.stageWidth;
private const SH:Number = stage.stageHeight;
private var drawArea:Sprite = new Sprite();
private var headerP:Sprite = new Sprite();
private var p:Walker = new Walker();
private var myStage:Sprite = new Sprite();
public function MouseFollower() {
addChild(myStage);
myStage.addChild(drawArea);
p.myX = SW/2;
p.myY = SH/2
p.mySpeed = 8;
p.myRad = 0;
p.myWeight = 2;
p.myColor = 0xFFFFFF;
headerP.graphics.beginFill(0xFFFFFF, 1);
headerP.graphics.moveTo(0,-17);
headerP.graphics.lineTo(6,0);
headerP.graphics.lineTo(-6,0);
headerP.graphics.lineTo(0,-17);
headerP.graphics.endFill();
myStage.addChild(headerP);
headerP.x = SW/2;
headerP.y = SH/2;
buttonMode = true;
addEventListener(MouseEvent.CLICK, myWalk);
addChild(new Stats());
}
private function myWalk(evt:MouseEvent):void {
removeEventListener(MouseEvent.CLICK, myWalk);
p.graphics.lineStyle(p.myWeight, p.myColor, 1)
p.graphics.moveTo(p.myX, p.myY);
drawArea.addChild(p);
p.addEventListener(Event.ENTER_FRAME, xMove);
addEventListener(MouseEvent.CLICK, myStop);
}
private function xMove(evt:Event):void{
var p:Object = evt.target;
var myV:Number = p.myRad-Math.atan2(mouseY - SH/2, mouseX - SW/2);
if(myV > Math.PI){
myV -= Math.PI*2;
} else if(myV < -Math.PI){
myV += Math.PI*2;
}
p.myRad -= myV/5;
p.myRad %= 2*Math.PI;
var myDist:Number = Math.sqrt((SW/2-mouseX)*(SW/2-mouseX)+(SH/2-mouseY)*(SH/2-mouseY))
p.mySpeed = myDist/20;
p.vX = p.mySpeed * Math.cos(p.myRad);
p.vY = p.mySpeed * Math.sin(p.myRad);
p.myX += p.vX;
p.myY += p.vY;
p.graphics.lineStyle(p.myWeight, p.myColor, 1)
p.graphics.lineTo(p.myX, p.myY);
myStage.x = -p.myX + SW/2;
myStage.y = -p.myY + SH/2;
headerP.x = p.myX;
headerP.y = p.myY;
headerP.rotation = (p.myRad*360)/(2*Math.PI) + 90;
}
private function myStop(evt:MouseEvent):void{
p.removeEventListener(Event.ENTER_FRAME, xMove);
removeEventListener(MouseEvent.CLICK, myStop);
addEventListener(MouseEvent.CLICK, myWalk);
}
}
}
import flash.display.Sprite;
class Walker extends Sprite {
public var myX:Number;
public var myY:Number;
public var vX:Number;
public var vY:Number;
public var mySpeed:Number;
public var myRad:Number;
public var myWeight:Number;
public var myWidth:Number;
public var myColor:Number;
public function Walker(){
}
}