AS3: removing objects by array item reference

断了今生、忘了曾经 提交于 2019-12-25 02:11:33

问题


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


回答1:


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




回答2:


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/...)



来源:https://stackoverflow.com/questions/1992456/as3-removing-objects-by-array-item-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!