Problem with removeChild in as3 (Error #2025)

[ 2009-01-24 14:10:26 | Author: liuhuan ]
Font Size: Large | Medium | Small
Many people might encounter the problem called Error 2025 when removing a display object from stage in as3. I got this error today and the code reads like below:
Quote
var a:Sprite = new Sprite();
addChild(a);
a.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
function onEnterFrameHandler(e:Event) {
  a.x++;
  if (a.x>20) {
    removeChild(a);
  }
}
function onMouseDownHandler(e:MouseEvent) {
  removeChild(a);
}
Most of the 2025 error occours when we try to remove an object which is no longer exist. So, once you get that error, be sure to check your eventlisteners, see if there's anything remaining. Coz even if we remove the object from the stage, the event listener still works, as the object is still in the virtual machine. Another way is to judge whether the object still exists in the display list before you remove it.

revised code:
Quote
var a:Sprite = new Sprite();
addChild(a);
a.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
function onEnterFrameHandler(e:Event) {
  a.x++;
  if (a.x>20) {
    if (this.contains(a)) {
      removeChild(a);
    }
  }
}
function onMouseDownHandler(e:MouseEvent) {
  if (this.contains(a)) {
    removeChild(a);
  }
}
Comments Feed Comments Feed: http://www.liuhuan.com/blog/feed.asp?q=comment&id=935

There is no comment on this article.

You can't post comment on this article.