20120605 角色操作(鍵盤&滑鼠)
var tx,ty;
var vx,vy;
var d ,dx,dy;
var spd = 4;
var State:String = "walk";
var mTimer:Timer = new Timer(100,0);
tx = player.x;
ty = player.y;
mTimer.start();
stage.addEventListener(MouseEvent.MOUSE_MOVE,mov)
function mov(e:MouseEvent)
{
player.play();
tx = mouseX;
ty = mouseY;
vx = tx - player.x;
vy = ty - player.y;
if (vx > 0)
player.rotation = Math.atan(vy/vx)*(180/Math.PI);
else
player.rotation = Math.atan(vy/vx)*(180/Math.PI)+180;
d = Math.sqrt(Math.pow(vx,2)+Math.pow(vy,2));
dx = vx*spd/d;
dy = vy* spd /d;
}
mTimer.addEventListener(TimerEvent.TIMER,moving)
function moving(e:TimerEvent)
{
var nd = Math.sqrt(Math.pow(tx-player.x,2)+Math.pow(ty-player.y,2));
if (nd >= spd)
{
player.x += dx;
player.y += dy;
}
else
{
player.x = tx;
player.y = ty;
player.stop();
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN,trainsit)
function trainsit(e:KeyboardEvent)
{
if(e.keyCode == 32)
{
if(State == "walk")
{
State = "fly";
spd = 12;
player.gotoAndPlay("fly");
}
else
{
State = "walk";
player.gotoAndPlay(State);
spd = 4;
}
}
}