角度转向-鼠标跟随
1,构建精灵
Arrow.as
package
{
import flash.display.Sprite;
public class Arrow extends Sprite
{
public function Arrow()
{
init();
}
public function init():void
{
graphics.lineStyle(1,0,1);
graphics.beginFill(0xffff00);
graphics.moveTo(-50,-25);
graphics.lineTo(0,-25);
graphics.lineTo(0,-50);
graphics.lineTo(50,0);
graphics.lineTo(0,50);
graphics.lineTo(0,25);
graphics.lineTo(-50,25);
graphics.lineTo(-50,-25);
graphics.endFill();
}
}
}
package
{
import flash.display.Sprite;
public class Arrow extends Sprite
{
public function Arrow()
{
init();
}
public function init():void
{
graphics.lineStyle(1,0,1);
graphics.beginFill(0xffff00);
graphics.moveTo(-50,-25);
graphics.lineTo(0,-25);
graphics.lineTo(0,-50);
graphics.lineTo(50,0);
graphics.lineTo(0,50);
graphics.lineTo(0,25);
graphics.lineTo(-50,25);
graphics.lineTo(-50,-25);
graphics.endFill();
}
}
}
2,跟随动画
FollowMouse.as
package
{
import flash.events.Event;
import flash.display.Sprite;
public class FollowMouse extends Sprite
{
private var myArrow:Arrow;
public function FollowMouse ()
{
Init ();
}
private function Init ():void
{
myArrow=new Arrow() ;
addChild (myArrow);
myArrow.scaleX=0.5;
myArrow.scaleY=0.3;
myArrow.x=stage.stageWidth / 2;
myArrow.y=stage.stageHeight / 2;
myArrow.addEventListener (Event.ENTER_FRAME,Active);
}
private function Active (e:Event):void
{
var vx:Number=0;
var vy:Number=0;
var ax:Number=(mouseX - myArrow.x)/5;
var ay:Number=(mouseY - myArrow.y)/5;
vx+= ax;
vy+= ay;
myArrow.x+= vx;
myArrow.y+= vy;
var angle:Number=Math.atan2(ay,ax);
myArrow.rotation=angle*180/Math.PI;
}
}
}
package
{
import flash.events.Event;
import flash.display.Sprite;
public class FollowMouse extends Sprite
{
private var myArrow:Arrow;
public function FollowMouse ()
{
Init ();
}
private function Init ():void
{
myArrow=new Arrow() ;
addChild (myArrow);
myArrow.scaleX=0.5;
myArrow.scaleY=0.3;
myArrow.x=stage.stageWidth / 2;
myArrow.y=stage.stageHeight / 2;
myArrow.addEventListener (Event.ENTER_FRAME,Active);
}
private function Active (e:Event):void
{
var vx:Number=0;
var vy:Number=0;
var ax:Number=(mouseX - myArrow.x)/5;
var ay:Number=(mouseY - myArrow.y)/5;
vx+= ax;
vy+= ay;
myArrow.x+= vx;
myArrow.y+= vy;
var angle:Number=Math.atan2(ay,ax);
myArrow.rotation=angle*180/Math.PI;
}
}
}