2013-04-22 72 views
0

你好我試圖創建一個類似於太空入侵者的遊戲,並且很難創建hitTestObject,所以如果外星人擊中mc_gun,玩家會失去生命,並且一旦他失去了三個生命遊戲結束後被帶到一個屏幕,他/她可以輸入他們的名字,將其帶入高分表。AS3 hitTestObject結束遊戲

我還是要做到以下幾點,但不知道從哪裏開始...

創建玩家將3和靜物。 當所有生命都消失時結束遊戲。 採取輸入名稱屏幕 高分表。

任何幫助將不勝感激。

這是我的文檔正文編碼...

stop(); 
var score:Number = 0; 
var livesLeft:Number = 3; 

var lifesArray:Array = new Array(); 
lifesArray.push(life1); 
lifesArray.push(life2); 
lifesArray.push(life3); 

addEventListener(Event.ENTER_FRAME, pulse); 

function pulse(event:Event):void 
{ 
    checkForHitOnGun(); 
    checkForHitOnAliens(); 
    checkForBulletOnBulletHit(); 
    //checkEndGameCondition 
} 

function checkForHitOnGun() 
{ 
    for (var i=0; i<numChildren; i++) 
    { 
     if (getChildAt(i) is alienbullet) 
     { 
      var mc_alienbullet:MovieClip = getChildAt(i) as MovieClip; 
      for (var j=0; j<numChildren; j++) 
      { 
       if (getChildAt(j) is gun) 
       { 
        var mc_gun:MovieClip = getChildAt(j) as MovieClip; 
        if (mc_alienbullet.hitTestObject(mc_gun)) 
        { 

        health -= 10; 
        if (health<1){ 
         if(livesLeft<1){ 
          gotoAndPlay(2); 
          removeChild(mc_gun); 
          stage.removeEventListener(Event.ENTER_FRAME,pulse); 


         } 
         livesLeft--; 
         lifesArray[livesLeft].visible = false; 

         health=100; 
         if (livesLeft == 0){ 
          (endGo); 
          gotoAndPlay(2); 
         } 
        } 
        trace("health =" +health); 
        } 
       } 
      } 
     } 
    } 
} 


function checkForHitOnAliens() 
{ 
    for (var i=0; i<numChildren; i++) 
    { 
     if (getChildAt(i) is bullet) 
     { 
      var mc_bullet:MovieClip = getChildAt(i) as MovieClip; 
      for (var j=0; j<numChildren; j++) 
      { 
       if (getChildAt(j) is alien) 
       { 
        var mc_alien:MovieClip = getChildAt(j) as MovieClip; 
        if (mc_bullet.hitTestObject(mc_alien)) 
        { 
         var tobeDestroyed:MovieClip; 
         if (mc_alien.x > mc_bullet.x) 
         { 
          tobeDestroyed = mc_alien; 
         } 
         else 
         { 
          tobeDestroyed = mc_alien; 
         } 
         var bomb_mc:explosion = new explosion(); 
         bomb_mc.x = tobeDestroyed.x; 
         bomb_mc.y = tobeDestroyed.y; 

         var addspaceship:alien = new alien(); 

         addspaceship.x = 600; 
         addspaceship.y = Math.random()*(stage.stageHeight); 

         tobeDestroyed.destroy(); 
         addChild(bomb_mc); 
         addChild(addspaceship); 
         score++; 

         return; 
        } 
       } 
       tb_score.text = "" + score; 
      } 
     } 
    } 
} 

function checkForBulletOnBulletHit() 
{ 
    for (var i=0; i<numChildren; i++) 
    { 
     if (getChildAt(i) is bullet) 
     { 
      var mc_bullet:MovieClip = getChildAt(i) as MovieClip; 
      for (var j=0; j<numChildren; j++) 
      { 
       if (getChildAt(j) is alienbullet) 
       { 
        var mc_alienbullet:MovieClip = getChildAt(j) as MovieClip; 
        if (mc_bullet.hitTestObject(mc_alienbullet)) 
        { 
         var tobeDestroyed:MovieClip; 
         if (mc_bullet.x > mc_alienbullet.x) 
         { 
          tobeDestroyed = mc_bullet; 
         } 
         else 
         { 
          tobeDestroyed = mc_alienbullet; 

         } 
         /*score++; 
         tb_score.text = "" + score;*/ 
         var bomb_mc:explosion = new explosion(); 
         bomb_mc.x = tobeDestroyed.x; 
         bomb_mc.y = tobeDestroyed.y; 

         tobeDestroyed.destroy(); 
         addChild(bomb_mc); 
         /*if (mc_bullet.hitTestObject(mc_alienbullet)) 
         { 
         score++; 
         mc_bullet.gotoAndPlay(2); 
         }*/ 
         score++; 

         return; 
        } 
        tb_score.text = "" + score; 
       } 
      } 
     } 
    } 
} 
//mc_gun would make the bullet fire only if the gun is clicked. 
//the game is to difficult so i changed it to stage. to make the whole stage the fireing area. 
stage.addEventListener(MouseEvent.CLICK, fire); 

function fire(event:MouseEvent):void 
{ 
    var bulletfire:bullet = new bullet(); 
    bulletfire.x = mc_gun.x - 20 + mc_gun.width; 
    bulletfire.y = mc_gun.y; 
    addChild(bulletfire); 
} 

mc_banner.addEventListener(MouseEvent.CLICK, startGo); 

function startGo(event:MouseEvent):void 
{ 
    mc_banner.visible = false; 
    mc_instructions.visible = false; 
    mc_highscore.visible = false; 
    mc_gun.visible = true; 
    tb_score.visible = true; 
    addEventListener(Event.ENTER_FRAME, pulse); 
    var initialspaceship:alien=new alien(); 

    initialspaceship.x = 600; 
    initialspaceship.y=Math.random()*(stage.stageHeight); 
    addChild(initialspaceship); 

} 

function endGo() 
{ 
    mc_gun.visible = false; 
    tb_score.visible = false; 
    removeEventListener(Event.ENTER_FRAME, pulse); 
    mc_banner.visible = true; 
    livesLeft--; 
    lifesArray[livesLeft].visible = false; 
} 
+0

你試過了嗎?你在哪裏墜落?什麼是對的,哪裏出了問題?請更具體地說一下你的問題,因爲你的問題是廣泛的... – NemoStein 2013-04-22 15:16:42

+0

@NemoStein我已經有了人生的工作是玩家獲得三條生命,但是當生命全部消失時,我會用什麼編碼結束遊戲並清除屏幕上的所有元素,然後將其帶到顯示輸入名稱的新屏幕以添加到高分表中。 – DP187 2013-04-22 15:41:22

回答

0

engGo()功能是一個負責遞減的生活。
這是最好的地方檢查玩家是否死了(0人生)並結束遊戲。
正如我可以理解的讀取您的代碼,livesLeft是您存儲玩家生活的地方。
剛過了lifesArray[livesLeft].visible = false;添加以下內容:

if (livesLeft <= 0) 
{ 
    defeat(); 
} 

現在你必須從遊戲切換到高分。
如果你是在一個框架中的Flash工作專業,你只需要移動到一個與高分

function defeat():void 
{ 
    gotoAndStop("HighScore"); 
} 

否則(你需要做的這一切在一幀),你需要清除階段並在其上添加新的東西。

function defeat():void 
{ 
    // First, remove ALL listeners 
    removeEventListener(Event.ENTER_FRAME, pulse); 
    stage.removeEventListener(MouseEvent.CLICK, fire); 

    // Then, we remove everything 
    removeChild(...); 

    // Finally, you add the new stuff 
    addChild(...); 
} 

如果你使用第二個方法去,我建議你去耦的功能和類包裝他們,所以你的遊戲邏輯將全部在一個地方(一個班),你的高分數屏幕將在另一個,你可以輕鬆地交換。

+0

我編輯了代碼來顯示我的生活,但我認爲這是不正確的?你說要在endGo中添加lifes部分,我該怎麼做...?我真的很感激你的幫助 – DP187 2013-04-22 16:40:50

+0

你能幫我創造生活嗎?我瞭解功能失敗,但無法弄清楚如何創建人生 – DP187 2013-04-22 18:33:35

+0

@ DP187,你是什麼意思「創造生活」? – NemoStein 2013-04-22 19:55:50