2013-03-23 17 views
0

嗨,我有一個問題,我想我可能知道是什麼造成它,但我不知道如何解決它。我任何人都可以幫我解決這個問題,這將是偉大的...的錯誤是錯誤#1009在ActionScript 3中,當我嘗試刪除子彈如果它擊中敵人

TypeError: Error #1009: Cannot access a property or method of a null object reference. at Bullet/removeSelf()[C:\Users\Alan\Desktop\game copy\Bullet.as:56] at Bullet/loop()[C:\Users\Alan\Desktop\game copy\Bullet.as:44]

這裏是主要行動,消除子彈PS THE ONE的代碼。對不起帽子。

stage.addEventListener(Event.ENTER_FRAME, testCollisions); 

//Check for collisions between an enemies array and a Lasers array 
function testCollisions(e:Event):void 
{ 

    var tempEnemy:MovieClip; 
    var tempLaser:MovieClip; 

    for (var i:int=enemies.length-1; i >= 0; i--) 
    { 
     tempEnemy = enemies[i]; 
     for (var j:int=bullets.length-1; j>=0; j--) 
     { 
      tempLaser = bullets[j]; 
      if (tempLaser.hitTestObject(tempEnemy)) 
      { 

       removeChild(tempEnemy); 
       removeLaser(j); 

      } 
     } 
    } 
} 




function removeLaser(idx:int) 
{ 
    parent.removeChild(bullets[idx]); 
    bullets.splice(idx,1); 
} 

這裏是Bullet類的地方刪除它

public class Bullet extends MovieClip { 

     private var speed:int = 30; 
     private var initialX:int; 
     public var eligableForRemove:Boolean = false; 



     public function Bullet(playerX:int, playerY:int, playerDirection:String) { 

      // constructor code 
      if(playerDirection == "left") { 
       speed = -30; //speed is faster if player is running 
       x = playerX - 25; 
      } else if(playerDirection == "right") { 
       speed = 30; 
       x = playerX + 25 
      } 
      y = playerY - 75; 

      initialX = x; //use this to remember the initial spawn point 

      addEventListener(Event.ENTER_FRAME, loop); 
     } 

     public function loop(e:Event):void 
     { 
      //looping code goes here 
      x += speed; 

      if(speed > 0) { //if player is facing right 
       if(x > initialX + 640) { //and the bullet is more than 640px to the right of where it was spawned 
        removeSelf(); //remove it 
        eligableForRemove = true; 
       } 
      } else if (speed < 0) { //else if player is facing left 
       if(x < initialX - 640) { //and bullet is more than 640px to the left of where it was spawned 
        removeSelf(); //remove it 
        eligableForRemove = true; 
       } else { 
        eligableForRemove = false; 
        } 
      } 
     } 

     public function removeSelf():void 
     { 
      if(eligableForRemove == true){trace("remove self"); 
      removeEventListener(Event.ENTER_FRAME, loop); //stop the loop 
      this.parent.removeChild(this); //tell this object's "parent object" to remove this object 
      //in our case, the parent is the background because in the main code we said: 
      //back.addChild(bullet); 
      } 

     } 

    } 

} 

我認爲是什麼原因造成它的代碼,它調用虛函數removeSelf時沒有要刪除。所以我添加了eligableForRemove變量,但我可能沒有正確放置它,所以如果任何人可以請幫我解決這個問題,我將不勝感激......另外,如果我嘗試從主Actions中刪除子彈,它給了我它必須來電錯誤的孩子。請幫忙。

回答

0

你說得對,這可以通過嘗試刪除子兩次引起的,你設置後eligableForRemove你打電話removeSelf(),所以removeSelf()不會看到更新後的值。

不過,我認爲問題是,你在removeLaser()取出子彈,但告訴子彈,它已經死了,所以它一直運行的循環,並最終它會出界,並嘗試再次移除自己。

您應該能夠擺脫eligableForRemove,只是改變removeLaser()於:(或者,如果bulletsVector.<Bullet>你可以做bullets[idx].removeSelf(),作爲運行時已經知道這是一個Bullet

function removeLaser(idx:int) 
{ 
    (bullets[idx] as Bullet).removeSelf(); 
    bullets.splice(idx,1); 
} 

+0

哦,是的,這解決了它,謝謝你! – AlanZ2223 2013-03-23 19:01:16

+0

嘿,你介意幫助我與別的東西嗎? – AlanZ2223 2013-03-24 00:03:52

+0

我剛剛走了,但是我會留意明天是否還有其他問題。 – 2013-03-24 01:30:33

相關問題