2017-04-23 60 views
0

我正在製作一個小遊戲。我的奴才的實例名稱是sideMinion。香蕉落下,一切正常,但我的removeChild()不能正常工作。我收到的錯誤是錯誤#2025:提供的DisplayObject必須是調用者的子項。 removeChild()或hitTestObject不能正常工作。AS3 ArgumentError:錯誤#2025:提供的DisplayObject必須是調用者的子女

這就是我在我的香蕉作爲。類:

package { 
    import flash.display.MovieClip; 
    import flash.utils.Timer; 
    import flash.events.TimerEvent; 
    import flash.events.MouseEvent; 
    import flash.events.Event; 
    import flash.events.Event; 

    public class banana extends MovieClip { 

     var velX: Number = 0; 
     var velY: Number = 0; 
     var falling: Boolean = false; 
     var gravity: Number = 0; 

     public function banana() { 
      var timing: Timer = new Timer(25, 0); 
      timing.addEventListener(TimerEvent.TIMER, moveMe); 
      timing.start(); 

     } 

     private function moveMe(event: TimerEvent) 
    { 
     x += velX; 
     y += velY; 

     if (falling) velY += gravity; 

     /* trace("[BANANA] position:", x, y, "speed:", velX, velY);*/ 
    } 


     public function setSpeed(dx,dy) { 
      velX = dx; 
      velY = dy; 
     } 

      public function setSpot(atX,atY){ 
      this.x=atX; 
      this.y=atY; 
     } 


     public function makeItFall(){ 
      falling=true; 
     } 


    } 

} 

而且在我的主要程序:

import flash.display.MovieClip; 
import flash.events.KeyboardEvent; 
import flash.events.Event; 

var leftKey:Boolean; 
var rightKey:Boolean; 
var upKey:Boolean; 
var downKey:Boolean; 
var jump:Boolean = false; 
var xvelocity:int = 9; 
var yvelocity:int = 3; 
var gravity:Number = 7; 


stage.addEventListener(Event.ENTER_FRAME, changeVelocity); 
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp); 
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown); 

    function changeVelocity(evt:Event){ 
     moveMinion(); 
     yvelocity += gravity; 
    } 

    function moveMinion(){ 

     if (leftKey == true){ 
      sideMinion.x -= xvelocity; 
      sideMinion.left(); 

     } 
     if (rightKey == true){ 
      sideMinion.x += xvelocity; 
      sideMinion.right(); 
     } 

    } 

    function checkKeyDown(e:KeyboardEvent){ 
     if (e.keyCode == Keyboard.LEFT){ 
      leftKey = true; 
     } 
     else if (e.keyCode == Keyboard.RIGHT){ 
      rightKey = true; 
     } 

    } 

    function checkKeyUp(e:KeyboardEvent){ 
     if (e.keyCode == Keyboard.LEFT){ 
      leftKey = false; 
     } 
     else if (e.keyCode == Keyboard.RIGHT){ 
      rightKey = false; 
     } 

    } 

    btnStart.addEventListener(MouseEvent.CLICK, makeItFall); 

    function makeItFall(e:MouseEvent){ 



    var numBananas = 6; 
    var theBananas: Array = new Array(); 
    theBananas = []; 
    for (var i = 0; i < numBananas; i++) { 
    var aBanana: banana = new banana(); 
    theBananas.push(aBanana); 
    btnStart.visible=false; 
    aBanana.y=30; 
    theBananas[i].setSpot(Math.random()*450+50,Math.random()*200+20); 
    theBananas[i].setSpeed((Math.random()), 1); 
    stage.addChild(aBanana); 

} 

var health: uint= 1; 

addEventListener(Event.ENTER_FRAME, pickUpBananas); 

function pickUpBananas(event:Event){ 
    for(var i= 0; i<theBananas.length; ++i){ 
     if (sideMinion.hitTestObject(theBananas[i])){ 
      removeChild(theBananas[i]); 
      health=health+1; 
      trace(health); 
     } 
    } 

} 

    } 


stop(); 
+0

既然你'stage.addChild(...'嘗試removeChild也爲階段:'stage.removeChild(...'。但是,你可能也想刪除從Bananas -array刪除香蕉?如果是這樣,在那種情況下,我也會把這個循環向後運行:for(i = theBananas.length-1; i> -1; i - )' – kaarto

回答

1

正如你所添加的孩子的階段,你必須也Stage中刪除:

stage.removeChild(theBananas[i]); 

對於未來,在某些情況下,如果您不知道實際的父級,則還可以使用父級財產:

theBananas[i].parent.removeChild(theBananas[i]); 

在你的遊戲中,我假設你也想從舞臺上刪除香蕉,以便你的數組不會因爲刪除香蕉而結束。所以,這裏的情侶修改:

for(var i:int = theBananas.length-1; i>-1; i--){ //inverted loop 
    if (sideMinion.hitTestObject(theBananas[i])){ 
     stage.removeChild(theBananas[i]); 
     theBananas.splice(i,1); //removing it from the array 
     health=health+1; 
     trace(health); 
    } 
} 

反向循環顯然從最後一個元素循環一路到第一個,becouse如果你想從數組中刪除第一個元素,那麼第二個元素將「跳」到了地點和循環會跳過它。

我希望我們都會很快看到你的遊戲! :)

+0

謝謝你的解釋。真的很有道理,我也希望每個人都能看到遊戲:) – Catalina

相關問題