2017-05-02 23 views
0

我正在開發一個項目,如果用戶點擊「種植花園」,它將生成隨機數量的花朵和隨機數量的雜草。一旦他們完成了「成長」序列,用戶就會看到一個「種植新花園」,其中的花朵/雜草將從舞臺上刪除並重新開始。如何刪除按鈕按下的孩子/精靈?

我已經絞盡腦汁,這是最接近我去「去除兒童」 - 我從來沒有理解的概念哈哈。任何幫助/指導,非常感謝。

**編輯的代碼,因爲我複製的舊文件**

import flash.events.MouseEvent; 
import flash.display.DisplayObject; 

// stops the playhead on frame 1 
stop(); 

// random amount of flowers generated 
var flowerAmount:int = (Math.ceil((Math.random() * 20)) + 9); 
var weedAmount = Math.ceil((Math.random() * 10)); 


// garden display container 
var newGarden:DisplayObjectContainer; 

// setting new flower variable equal to the function that creates an instance of the flower 
var newFlower_mc:DisplayObject = newFlower(); 

// flowers currently in the garden 
var flowersInGarden:int = 0; 
var weedsInGarden:int = 0; 


// event listener for the grow button to start the garden 
grow_btn.addEventListener(MouseEvent.MOUSE_UP, frameTwo); 

// when grow button is clicked go to frame two 
function frameTwo(event:MouseEvent) { 
    gotoAndPlay(2); 
} 

// changes the size and position of the flower 
function configureFlower(myFlower_mc:DisplayObject) { 
    myFlower_mc.x = Math.random() * 400; 
    myFlower_mc.y = Math.random() * 200; 
    var flowerSize:Number = Math.random() + .5; 
    myFlower_mc.height = myFlower_mc.height * flowerSize; 
    myFlower_mc.width = myFlower_mc.width * flowerSize; 
} 
import flash.display.DisplayObject; 

// function to create new instance of a flower 
function newFlower():DisplayObject { 
    var newFlower_mc:DisplayObject = new flower(); 
    return newFlower_mc; 
} 

// function to call the create flower function and add flower to sprite 
function createFlower() { 
    var myFlower_mc = newFlower(); 
     configureFlower(myFlower_mc); 
     newGarden.addChild(myFlower_mc); 
    trace(flowerAmount); 
    } 

newGarden = new Sprite(); 

// adds the flower to the stage/sprite and adds to the flower counter 
    function showFlowers() { 
    createFlower(); 
    addChild(newGarden); 
    flowersInGarden++; 
    trace("Flowers:" + flowersInGarden + " " + weedAmount + " weedsingarden" + weedsInGarden); 


} 
// calls the above function 
showFlowers(); 


// function to create a weed, configure weed and add to the garden sprite 
function createWeed(){ 
    var newWeed:DisplayObject; 
    trace("creating weed"); 
    newWeed = new weed(); 
    newGarden.addChild(newWeed); 
    configureFlower(newWeed); 
    weedsInGarden++; 
}// if all the flowers haven't grown yet, go back to frame 2 until they have 

if (flowersInGarden < flowerAmount) { 
    gotoAndPlay(2); 
} 

// if the amount of weeds decided haven't grown yet, create another weed 

if (weedsInGarden < weedAmount){ createWeed(); }; 
stop(); 

// event listener to grow a new garden 
new_btn.addEventListener(MouseEvent.MOUSE_UP, growNewGarden); 


// function to create a new garden if there are more than 1 instance in the container 
function growNewGarden(event:MouseEvent) { 
    while (newGarden.numChildren > 0) { 
    stage.removeChild(newFlower_mc); 
     stage.removeChild(newGarden); 
     // add a new, fresh sprite 
     stage.addChild(newGarden); 
     // randomly chooses a number of flowers 
     flowerAmount = (Math.ceil((Math.random() * 21)) + 10); 
     // resets flower counter to zero 
     flowersInGarden = 0; 
    gotoAndPlay(2); 

}} 

回答

1

如果您打算通過使用numChildren屬性作爲你的財產循環,你應該使用子指標作爲變量循環來選擇孩子。你所做的是你循環通過孩子,並試圖刪除名爲newFlower_mc或類似的東西,該變量並不意味着該功能的範圍內的任何東西。它可能看起來像這樣

for (var i:int = newGarden.numChildren - 1; i >= 0; i--){ 
    var f:DisplayObject = newGarden.getChildAt(i); 
    f.parent.removeChild(f); 
} 

所以這是一種方法。

我認爲只要將花園拆除就會簡單多了,因爲所有的雜草和花都是花園的孩子,所以它們也會被移除。簡單地說:

newGarden.parent.removeChild(newGarden); 

瞧!

此外,只是關於您的var newFlower_mc = newFlower();一個旁註。我不確定你明白你在做什麼。它不會設置一個等於一個函數的變量。它將該變量設置爲等於輸出或該函數的結果被調用一次。該行代碼是這樣的:

  1. 聲明一個名爲newFlower_mc的獨特名稱的變量。
  2. 調用構造函數newFlower()它返回返回DisplayObject實例變量newFlower_mc

這一切都很好一個DisplayObject

  • 集,但你有問題。稍後,在構造函數中,如果newFlower,則聲明另一個變量...,其名稱已被使用... newFlower_mc。這不好。我不知道結果是什麼。如果閃光是以某種方式允許這個編譯和運行沒有錯誤,我很驚訝,但無論如何這是不好的做法。我認爲它來自你不知道當你設置一個變量=函數時會發生什麼。我希望我的解釋有幫助。

  • +0

    我從我的教科書中的前一個示例代碼,所以也許我誤用了它。 我會在早上看看 - 感謝您花時間抽出時間給出詳細的答案 - 非常感謝! – Lexii