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