Ich arbeite an einem Projekt, bei dem der Benutzer, wenn er auf "Garten wachsen" klickt, eine zufällige Anzahl von Blumen und eine zufällige Anzahl von Unkräutern erzeugt. Sobald sie ihre "wachsende" Sequenz abgeschlossen haben, wird dem Benutzer ein "Wachsen Sie einen neuen Garten" präsentiert, in dem die Blumen/Unkräuter von der Bühne gelöscht werden und von neuem beginnen.So entfernen Sie Kinder/Sprites beim Drücken der Taste?
Ich habe mir den Kopf zerbrochen und das ist das nächste, was ich zum "Entfernen der Kinder" habe - ein Konzept, das ich nie verstanden habe haha. Jede Hilfe/Anleitung wird sehr geschätzt.
** Edited-Code als ich kopiert die alte Datei **
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);
}}
Ich habe den Code aus einem früheren Beispiel in meinem Lehrbuch genommen, also habe ich ihn vielleicht falsch angewendet. Ich werde einen Blick auf den Morgen werfen - danke, dass Sie sich die Zeit genommen haben, eine ausführliche Antwort zu geben - sehr geschätzt! – Lexii