網路教學(e-Learning)校園社群(e-Community)服務台(e-Service)系所班網(e-Class)登入
2012.10.09 敵方攻擊
by 高鈺婷 2012-10-25 23:53:41, 回應(0), 人氣(501)

var bul : MovieClip;
var hp :int = 5;
var bulSpd : int = 20;
var bulwith : Boolean = false;
var movable : Boolean = true;

var vx, vy : Number;
var d, dx, dy : Number;

blood.gotoAndStop(1);
bul = new bullet();
//--------------
stage.addEventListener(Event.ENTER_FRAME, loop);
function loop (e:Event)
{
  vx = player.x - enemy.x;
  vy = player.y - enemy.y;
  enemy.rotation = getrot(vx, vy);
  d = Math.sqrt(Math.pow(vx, 2) + Math.pow(vy, 2));
  dx = (vx * bulSpd) / d;
  dy = (vy * bulSpd) / d;
  if (player.State == "super")
  {
   hp = 5;
   blood.gotoAndStop(1);
   movable = true;
  }
}

function getrop (vx : Number, vy : Number) : Number
{
  var rot : Number;
  if (vx > 0)
  rot = Math.atan (vy / vx) * (180 / 3.14159326);
  else
  rot = Math.atan (vy / vx) * (180 / 3.14159326) + 180;
  return rot;
}
//--------------
enemy.addEventListener(Event.ENTER_FRAME, shot);
function shot (e:Event)
{
  if (bulwith == false)
    {
   stage.addChild(bul);
   bul.rotation = getrot(vx, vy);
   bul.x = enemy.x + (enemy.width / 2) * Math.cos((bul.rotation) * (Math.PI / 180));
   bul.y = enemy.y + (enemy.width / 2) * Math.sin((bul.rotation) * (Math.PI / 180));
   bul.addEventListener(Event.ENTER_FRAME, hit);
   bulwith = true;
  }
  else
  {
   bul.x += dx;
   bul.y += dy;
   if (bul.y > 420)
   {
    stage.removeChild(bul);
    bul.removeEventListener(Event.ENTER_FRAME, hit);
    bulwith = false;
   }
  }
}

//------------

 

function hit (e:Event)
{
  if ((player.State == "alive") && (bul.hitTestObject(player)) && (hp > 0))
  {
   hp--;
   player.body.play();
   blood.nextFrame();
   bul.removeEventListener(Event.ENTER_FRAME, hit);
   stage.removeChild(bul);
   bulwith = false;
   if (hp == 0)
   {
    movable = false;
    player.gotoAndPlay("die");
   }
  }
}

 

//--------------

stage.addEventListener(KeyboardEvent.KEY_DOWN, mov);
function mov (e:KeyboardEvent)
{
  if (movable == true)
  {
   switch (e.keyCode)
   {
    case 37 :
    if (player.x - 10 >= 50)
    player.x -= 10;
    else
    player.x = 50;
    break;
    case 39 :
    if (player.x + 10 <= 500)
    player.x += 10;
    else
    player.x = 500;
   }
  }
}