AS3 - removal of two objects added to stage with the same variable name
I'm going to start by saying that i am very new to flash and the concept of Stage is still very new to me.
I have the following problem:
at a given time i have:var foo:MyClass() = new Class();
stage.addChild(foo);
...
foo = new myClass();
stage.addChild(object);
so now i have two or 开发者_Python百科more foo objects on stage. my variable is for one foo, and i need to remove them all from the stage.
what to do? thank you
I would say don't use the same variable for both objects.
That said, you can remove objects without a unique reference to them by looping through all the children. You should loop backwards when doing this, so that the counts don't mess up when you manipulate them. Code off the top of my head that is probably not exactly right:
for (var i = stage.numChildren; i > 0; i--) {
stage.remove(stage.getChildAt(i - 1));
}
While both objects definitely exist now, you've changed your foo reference value from one new MyClass to then, the next. And, as you've found, that reference value == exactly one object.
There are many ways to capture reference to these objects, beyond a variable name. @jhocking notes, correctly, that you may use the displayList's children
parameter to provide access (the display list is just an array-like stack, after all). This may create problems, however, if you have other items in the same display list context.
Another good alternative is to push the object into an array (or vector), which you can loop through at destroy time.
var objects:Array = [];
for (var i = 0; i< 10; i++){
var newObject:MyClass = new MyClass();
this.addchild(newObject as DisplayObject);
objects.push(newObject);
}
for (var j = objects.length; j > 0; j--){
this.removeChild(objects[j] as DisplayObject);
}
Hope that helps -
What you are doing is creating a variable, adding it to the stage and then throwing away the reference, I would recommend that you store the references in an array. In the class, add an array variable to hold them:
private var _foos:Array = [];
Now you can store a reference each time you create a Foo object:
var foo:MyClass() = new MyClass();
// Add it to the stage
stage.adddChild(foo);
// Store the reference
_foos.push(foo);
foo = new MyClass();
// Add it to the stage
stage.adddChild(foo);
// Store the reference
_foos.push(foo);
If you want to get rid of them all, just loop through the reference array:
for each (var foo:MyClass in _foos) {
stage.removeChild(foo);
}
// Empty the array
_foos = [];
Or, like jhocking said, you could just loop trough all the children of the stage in see if they are the type of your class:
for (var i = stage.numChildren-1; i >= 0; i--) {
var child:DisplayObject = stage.getChildAt(i);
if (child is MyClass)
stage.removeChild(child );
}
From what I can see is that you did not add 2 items to stage but instead you just reused the 1 object.
// here you instantiated the object
var foo:MyClass() = new Class();
// here you added the object to stage
stage.addChild(foo);
// here you re-instantiated the object leaving it attached to the stage
foo = new myClass();
// because it was already added to the stage you just caused it to move to the top of the display list
stage.addChild(object);
// so with all that being said doing the following code one time
stage.removeChild( foo );
// should remove foo from the stage.
精彩评论