Reputation: 764
I have a problem in ActionScript 3 with a project where I am to output a bunch of data in a diagram and as pure text. This is not the problem, though. The problem is that when the user changes the type of data he wants shown. I would then remove the currently shown columns in the diagram, and add new ones. But my code removes ALL columns, so that there is no diagram at all!
I've done it all in Flash CS5, and the columns are instances of an object I made (a rectangle) which I just add to the stage. All the instances are added to the container "container." When I want to remove them, and add new ones I use:
var container = new Sprite(); // The container for my diagram
function emptyContainer() {
if (container.numChildren > 0) {
container.removeChildAt(0);
}
}
Which works.
I have switch statement that adds the different columns based on user-input. The name of the object (the rectangle) is "soyle." The name of the instances is "stolpe" (I am norwegian by the way, so that's the deal with those weird words)
function fChange(event:Event) {
textToBeReturned = "";
switch (treCbox.selectedItem.label) {
case "Furu":
for (var a = 1; a < table.length; a++ ){
textToBeReturned += "I år: " + table[a][0] + " - " + table[a][1] + " millioner trær." + "\n";
// Diagram
var stolpe = new soyle;
stolpe.width = table[a][1];
stolpe.x = 20;
stolpe.y = 100 + (a * 30);
container.addChild(stolpe);
// Description for the columns
var textbox = new TextField;
textbox.text = "År " + table[a][0];
textbox.x = stolpe.width + 30;
textbox.y = stolpe.y;
container.addChild(textbox);
}
break;
case "Gran":
for (var b = 1; b < table.length; b++ ){
textToBeReturned += "I år: " + table[b][0] + " - " + table[b][2] + " millioner trær." + "\n" ;
var stolpe = new soyle;
stolpe.width = table[b][1];
stolpe.x = 20;
stolpe.y = 100 + (b * 30);
container.addChild(stolpe);
// Description for the columns
var textbox = new TextField;
textbox.text = "År " + table[b][0];
textbox.x = stolpe.width + 30;
textbox.y = stolpe.y;
container.addChild(textbox);
}
break;
case "Lauvtre":
for (var c = 1; c < table.length; c++ ){
textToBeReturned += "I år: " + table[c][0] + " - " + table[c][3] + " millioner trær." + "\n" ;
var stolpe = new soyle;
stolpe.width = table[c][1];
stolpe.x = 20;
stolpe.y = 100 + (c * 30);
container.addChild(stolpe);
// Description for the columns
var textbox = new TextField;
textbox.text = "År " + table[c][0];
textbox.x = stolpe.width + 30;
textbox.y = stolpe.y;
container.addChild(textbox);
}
break;
}
txtReturn.text = textToBeReturned;
};
I wonder where to place my function for removing all children, to remove just those children that were added earlier. To matter where I place the function (at the start of fChange, at the beginning of each case) there is NO diagram shown, either new or old.
There is of course an array with the data, but I do not post it as it is irrelevant.
Thank you for your time, and in advance for your help! :)
Upvotes: 2
Views: 109
Reputation: 6349
Couple of problems here. I assume you want to remove all children from your container MC before adding new children inside your switch. (Tip for next time, try to post only the code your working with. The switch statement here is large and overwhelming. It's also not really relevant)
Your function emptyContainer does not actually remove all children, it only removes one at index 0. This needs to be inside of a loop.
function emptyContainer() {
while (container.numChildren > 0) { //changed the if to a while. This will run until there are no children left in container
container.removeChildAt(0);
}
}
You're going to want to call this first thing inside of your fChanged function
function fChange(event:Event) {
emptyContainer()
/* rest of function */
}
try this out and let me know how it works.
Upvotes: 2