如何在按钮上删除儿童/精灵



我正在研究一个项目,如果用户点击"种植花园",它将产生随机数量的花和随机数量的杂草。一旦完成"生长"序列,用户就会出现"种植新花园",其中将从舞台上删除花朵/杂草并重新开始。

我已经大脑了,这是我最接近"去除孩子"的人 - 我从未理解的概念哈哈。非常感谢任何帮助/指导。

**编辑的代码在我复制旧文件**

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属性作为属性进行循环,则应将儿童索引用作循环中的变量来选择孩子。您所做的是,您正在循环浏览孩子,并试图删除所谓的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);

voila!

另外,关于您的var newFlower_mc = newFlower();的旁注。我不确定您知道这在做什么。它不设置等于函数的变量。它设置该变量等于输出或该函数的结果一次。那条代码执行此操作:

  1. 声明一个名为newFlower_mc的唯一名称的变量。
  2. 调用返回 DisplayObject
  3. 的构造函数函数 newFlower()
  4. 设置将DisplayObject实例返回到变量newFlower_mc

这一切都很好,但是您有问题。稍后,在您的构造函数函数中,如果newFlower,您会声明另一个变量...带有已经使用的名称... newFlower_mc。这不好。我不知道结果是什么。如果Flash以某种方式允许此编译并在没有错误的情况下进行编译和运行,我会感到惊讶,但无论如何,这是不好的做法。我认为这是您不理解当您设置可变=函数时会发生什么的。希望我的解释能有所帮助。

最新更新