如何在按下按钮时删除 child/sprites?

How to remove child/sprites on button press?

我正在做一个项目,如果用户点击 "grow a garden",它会生成随机数量的花和随机数量的杂草。一旦他们完成 'growing' 序列,用户将看到一个 "Grow a New Garden",其中 flowers/weeds 将从舞台中删除并重新开始。

我绞尽脑汁,这是我最接近的"removing the children"——一个我从未理解过的概念哈哈。非常感谢任何help/guidance。

** 在复制旧文件时编辑了代码**

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);

}}

如果你打算使用 numChildren 属性 作为你的 属性 循环,你应该使用 child 索引作为循环中的变量到select的children。您所做的是循环遍历 children 并尝试删除名为 newFlower_mc 或类似内容的内容,并且该变量在该函数的范围内没有任何意义。它可能看起来像这样

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

这是一种方式。

我认为只移除花园会简单得多,而且由于所有杂草和花卉都是花园的 children,因此它们也会被移除。只需:

newGarden.parent.removeChild(newGarden);

瞧!

另外,关于您的 var newFlower_mc = newFlower(); 的附注。我不确定你明白那在做什么。它没有将变量设置为等于函数。它将该变量设置为等于一次调用该函数的输出或结果。那行代码是这样做的:

  1. 声明一个具有唯一名称的变量,名为 newFlower_mc
  2. 调用构造函数 newFlower() 其中 returns a DisplayObject
  3. 将返回的 DisplayObject 实例设置为变量 newFlower_mc

这很好,但是你有问题。稍后,在您的构造函数 if newFlower 中,您声明了另一个变量……其名称已被使用……newFlower_mc。这是不好的。我不知道结果是什么。如果 flash 以某种方式允许它编译并且 运行 没有错误我很惊讶,但无论如何这是不好的做法。我认为这是因为您不了解将变量 = 设置为函数时会发生什么。我希望我的解释有所帮助。