我正在做一个项目,如果用户点击“种植花园”,它将生成随机数量的花和随机数量的杂草。一旦他们完成了他们的“种植”序列,用户就会看到一个“种植新花园”,其中的花/杂草将从舞台上删除并重新开始。
我绞尽脑汁,这是我离“带走孩子”最近的一次——一个我从来没有理解过的概念哈哈。非常感谢任何帮助/指导。
** 在我复制旧文件时编辑代码**
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);
瞧!
另外,只是关于您的var newFlower_mc = newFlower();
. 我不确定你明白那是做什么的。它不是将变量设置为等于函数。它将该变量设置为等于该函数被调用一次的输出或结果。这行代码是这样做的:
newFlower_mc
。newFlower()
,它返回一个DisplayObject
newFlower_mc
这一切都很好,但你有一个问题。后来,在你的构造函数,如果newFlower
,你声明另一个变量......与已在使用的名称... newFlower_mc
。这不好。我不知道结果是什么。如果 flash 以某种方式允许它编译和运行而没有错误,我很惊讶,但无论如何这是不好的做法。我认为这是因为您不了解将变量设置=
为函数时会发生什么。我希望我的解释有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句