views:

130

answers:

2

Hi there,

I am trying to add some Sprite objects as the contents of an array, and I would like to be able to "clear" them from the stage. I would assume that if there are loaders involved, I need to do

_imgArray[i].close();
_imgArray[i].unload();

And if I am using a sprite, I can do:

removeChild(_imgArray[i]);

None of the above work. WHY???

For an example and/or description of how I am setting this up, see Joel's post here ...but note that he hasn't included a reference for deleting them from view.

Currently I try:

for(i = 0; i < _localXML.length(); i++)
{
   var tmp:BMLink = new BMLink(_localXML[i], _bw, _bh, i);
   _imgArray[i] = tmp;
   _imgArray[i].x = (_bw + _mainpad) * i; 
   _base.addChild(_imgArray[i]);
}

But this doesn't work. I would love it if someone could explain to me why this wouldn't be proper syntax. The class instances that are populating the array are all extending sprite, but they have their own individual loaders inside w/ progress events etc.

jml

A: 

You can make an Interface IDestroy for example with a destroy method who will manage all cleaning/removing stuff :

public interface IDestroy{
 function destroy():void;
}

public class MySprite extends Sprite implements IDestroy {
 ..
 public function destroy():void{
  // remove events
  ..
  // remove loader
  ..
  //remove from parent
  if (parent!==null){
   parent.removeChild(this);
  }
  // etc.. more cleaning
 }
}

then when you have an object who is an instance of IDestroy you can call the destroy method

if (myObject is IDestroy){
 IDestroy(myObject).destroy();
}

or another way

var id:IDestroy=myObject as IDestroy;
if (id!==null)
   id.destroy();

Edit:

I don't understand why any of the method i gave you in the comment will not work but _base.removeChild(_imgArray[i]) will :

addChild and removeChild accept only a DisplayObject as a parameter, so if you can do _base.addChild(_imgArray[i]) it means that _imgArray[i] inherits from DisplayObject and _imgArray[i] has a parent.

So var myDisplayObject:DisplayObject=_imgArray[i] as DisplayObject; will not return null and you will be able todo myDisplayObject.parent.removeChild(myDisplayObject); which is a general approach to your problem without relying on your _base DisplayObjectContainer (MovieClip/Sprite/...)

Patrick
For removing the child from you array (i suppose that your display object are into the array ?):`var myDisplayObject:DisplayObject=_sprArray[i] as DisplayObject;if (myDisplayObject!==null) { if (myDisplayIObject.parent!==null){ myDisplayObject.parent.removeChild(myDisplayObject); }}`
Patrick
OK, but this doesn't work because I would want to instantiate the constructor for this. My my case the code looks like:var tmp:BMLink = _imgArray[i] as BMLink(_localXML[i], _bw, _bh, i);What can I change?
jml
If _imgArray[i] contain a Class you can do : new BMLink(_imgArray[i])(_localXML[i], _bw, _bh, i)
Patrick
I'm sorry; this doesn't work for me at all.I will amend my question.
jml
Normally this have to work if i understand well your pb:var tmp:BMLink = _imgArray[i] as BMLink;if ((tmp!==null)
Patrick
@Patrick: I don't feel that this is clear at all; you are not describing how I would get the constructor variables set and your previous example does NOT work. Please do not post any more to this question as it is just confusing the issue. If anyone else has other suggestions I would be happy to view them. Please post code as an answer rather than a comment. Thanks,jml
jml
A: 

OK; I finally figured it out through a bunch of trial and error. It seems that I was attempting to remove the child of my main class sprite (this) rather than the sub-sprite that I had added the children to.

Sorry for the noise, but for the record, if you find that you can't do

this.removeChild(_imgArray[i]);

it's not because you don't have the correct syntax, but because you might not have an

_imgArray[i] 

at that particular point of your display list hierarchy... so...

_base.removeChild(_imgArray[i]);

...worked in this case.

jml

jml
I have edited my answer to explain a bit my comment, so if you have try why i have suggest you, where did it failed ? Thanks.
Patrick