我正在开展一个项目,如果用户点击"种植花园",它将生成随机数量的花和随机数量的杂草。一旦他们完成了他们的成长'顺序,向用户呈现"成长新花园"将花/杂草从舞台上删除并重新开始。
我已经绞尽脑汁,这是我最接近的目标"去除孩子" - 我从未理解的概念哈哈。非常感谢任何帮助/指导。
**编辑代码,因为我复制了旧文件**
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);
}}
答案 0 :(得分: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();
的旁注。我不确定你明白这是做什么的。它没有将变量设置为等于函数。它设置该变量等于输出或该函数被调用一次的结果。这行代码执行此操作:
newFlower_mc
的唯一名称的变量。 newFlower()
DisplayObject
newFlower_mc
这一切都很好,但你有问题。稍后,在newFlower
的构造函数中,您声明另一个变量...,其名称已在使用中...... newFlower_mc
。这是不好的。我不知道结果是什么。如果flash以某种方式允许这种编译和运行没有错误我很惊讶,但无论如何这是不好的做法。我认为这是因为你不了解在设置变量=
函数时会发生什么。我希望我的解释有所帮助。