/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/jUX0
*/
package {
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
myPlayer = new aPlayer();
addChild(myPlayer);
myThing = new aThing();
addChild(myThing);
myThing.cx = 200;
myThing.cy = 200;
addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var myPlayer:aPlayer;
public var myThing:aThing;
public function onEnter(e:Event):void
{
myPlayer.x = mouseX;
myPlayer.y = mouseY;
//myThing.moveForward();
//myThing.ang += 0.1;
myThing.update();
}//onenter
}//classend
}
import flash.text.TextField;
import flash.display.Sprite;
internal class aPlayer extends Sprite
{
public static var p:aPlayer;
public function aPlayer()
{
p = this;
graphics.clear();
graphics.lineStyle(2,0);
graphics.drawRect(-8,-8,16,16);
}//ctor
}//aplayer
internal class aThing extends Sprite
{
public var player:aPlayer;
public var deb:TextField;
public function aThing()
{
graphics.clear();
graphics.lineStyle(2,0);
graphics.drawCircle(0,0,16);
graphics.moveTo(0,0);
graphics.lineTo(16,0);
player = aPlayer.p;
deb = new TextField();
addChild(deb);
}//ctor
public var cx:Number = 0;
public var cy:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public var nx:Number = 0;
public var ny:Number = 0;
public var sx:Number = 0;
public var sy:Number = 0;
public var ang:Number = 0;
public function update():void
{
var dist:Number;
dist= getPdist();
deb.text = ""+dist;
nx = Math.cos(ang);
ny = Math.sin(ang);
sx = Math.sin(ang) *-1;
sy = Math.cos(ang);
if (isPlayerFront())
{
moveBack();
}
else
{
moveForward();
}
if (isPlayerLeft())
{
ang -= 0.01;
}
else
{
ang += 0.01;
}
x = cx;
y = cy;
rotationZ = ang * (180.0/3.1415);
}//update
public function isPlayerFront():Boolean
{
var dist:Number;
dist = (player.x-x)*nx + (player.y-y)*ny;
return dist > 0;
}//ispfront
public function isPlayerLeft():Boolean
{
var dist:Number;
dist = (player.x-x)*sx + (player.y-y)*sy;
return dist > 0;
}//ispleft
public function getPdist():Number
{
var dx:Number;
var dy:Number;
var mag:Number;
dx = x - player.x;
dy = y - player.y;
mag = Math.sqrt(dx*dx + dy*dy);
return mag;
}//getpdist
public function moveForward():void
{
cx += Math.cos(ang);
cy += Math.sin(ang);
}//movefor
public function moveBack():void
{
cx -= Math.cos(ang);
cy -= Math.sin(ang);
}//moveback
}//athing