Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 07-29-2008, 08:02 AM   #1
evride
2006 Man Of The Year
 
evride's Avatar
 
Join Date: Mar 2007
Posts: 919
Default RemoveChild Solution - Error #2025

So we get a lot of questions on here asking about removing movieclips and getting #2025 errors. I've written a class to help the budding Actionscripters to help them get past the errors.

Error 2025:
The error happens when you try to remove an object that isn't add to the stage or you do not specify the correct parent to remove the movieclip from.


This class does two things. It will get the correct parent object of the DisplayObject you are trying to remove. It will also silence the error so that your movie can continue to play.

This should not be a permanent fix, but actually a stepping stone used to help you learn actionscript 3 better and to prevent you from ripping your hair out.

how to use:
ActionScript Code:
import com.evride.RemoveObject; var mc:MovieClip = new MovieClip(); RemoveObject.remove(mc); //doesn't work and just traces an error (continues to play) addChild(mc); //adds the movieclip to the stage RemoveObject.remove(mc); //works fine, traces nothing.


here is the class. save it in your Actionscript 3 classes folder under com\evride
ActionScript Code:
////////////////////////////////////////// //   written by evride                  // //   Actionscript.org forums user       // //   found a problem? tell me there     // ////////////////////////////////////////// package com.evride{     public class RemoveObject{         public static function remove(obj:Object) {             if (obj) {                 if (obj is DisplayObject) {                     if(obj.parent){                         try {                             obj.parent.removeChild(obj);                         } catch (e:Error) {                             trace("Couldn't remove the object. It wasn't a child of a DisplayObject.");                         }                     }else{                         trace("Couldn't remove object. Parent property is null.");                     }                 } else {                     trace("Couldn't remove object. It is not a DisplayObject.");                 }             } else {                 trace("Couldn't remove the object. It was null or doesn't exist.");             }         }     } }
Attached Files
File Type: zip RemoveObject.zip (527 Bytes, 449 views)
__________________
As long as people believe in absurdities they will continue to commit atrocities. --Voltaire

Last edited by evride; 12-22-2009 at 01:06 PM.
evride is offline   Reply With Quote
Old 07-29-2008, 12:14 PM   #2
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,875
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

You got some problems there.

1) obj should be typed as DisplayObject in the function parameters. These are the only objects that can be put on the display list.
2) hasOwnProperty does not check a property's value, only whether the property exists. The parent property of a DisplayObject will always exist. It is simply null when not on the display list.
3) The .name property can be "" meaning the if statement checking for name can fail for a DisplayObject even if it's on the display list
4) The getChildByName check is redundant. The parent property is readonly and automatically set by Flash. If parent is non-null, it is guaranteed to be the parent of obj. Additionally, that check could potentially return a false positive if the parent happened to have a movie clip with a similar name that wasn't the movie clip in question.

The simplest solution is simply
if (obj.parent) obj.parent.removeChild(obj);

: )
__________________
(6)
senocular is offline   Reply With Quote
Old 07-29-2008, 01:32 PM   #3
evride
2006 Man Of The Year
 
evride's Avatar
 
Join Date: Mar 2007
Posts: 919
Default

1.) I dont want any errors resulting from a "cannot convert" problem, it stays as Object then get checks for usual displayobject kinda properties.

2.) yes i forgot to check to make sure the parent property was not null. for some reason forgot to add that check after the hasOwn...
the hasOwnPropert("parent") will limit it to only DisplayObjects and objects that the user set a parent variable.

3.) this is another check to make sure its a displayObject since arrays and strings do not have name properties. works just like the parent check.

4.)ya, maybe. parent property is not read only on all objects tho and can be set by the user on Array and other non-displayObjects. it's been removed cause after checking with an array with name and parent set as strings, it produced the same result as if I had it without.

you're method does not stop or silence all errors that an absolute beginner may get from trying to remove a displayObject. That is the whole point of this class. It's also to reduce the number of repeat questions about this error so we can answer other questions. You're code also does not give feedback to the user about what happened. and, with my class, the user only has to write one line of code.
Thank you for pointing me to the errors tho.

the class has been updated.
__________________
As long as people believe in absurdities they will continue to commit atrocities. --Voltaire

Last edited by evride; 07-29-2008 at 02:23 PM.
evride is offline   Reply With Quote
Old 07-29-2008, 02:27 PM   #4
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,875
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

You can just check if (obj is DisplayObject) in the function then. This will account for null and non-display object values.
__________________
(6)
senocular is offline   Reply With Quote
Old 07-29-2008, 02:33 PM   #5
evride
2006 Man Of The Year
 
evride's Avatar
 
Join Date: Mar 2007
Posts: 919
Default

ok, cool, another good suggestion, thanks. fixed. I think it's perfect now.
__________________
As long as people believe in absurdities they will continue to commit atrocities. --Voltaire
evride is offline   Reply With Quote
Old 07-29-2008, 02:37 PM   #6
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,875
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

good job : )
__________________
(6)
senocular is offline   Reply With Quote
Old 08-20-2008, 03:59 AM   #7
limitedwave
Member
 
Join Date: Jun 2008
Posts: 54
Default

Thanks for this guys, I've been pounding on that 2025 error too much lately, and not fixing squat. I implemented your class, and it performed as prescribed. The error still persists of course, and I'm still interested is removing it completely. I add a datagrid to the stage, then with a series of buttons I swap out the datagrid for different ones. The first time you "change channels", in effect, swapping grids...no errors. Then after that the errors 2025 come every time, incrementing by one each time. The datagrids are not on the stage directly, but added on a movie clip. Does this sound like a normal error for a newbie like me to be getting? It doesn't hinder the actual user experience, but player overhead concerns me. You can see my player here:
http://www.titansonline.com/multimedia/

Thanks!
limitedwave is offline   Reply With Quote
Old 09-03-2010, 12:07 PM   #8
pisosse
Senior Member
 
Join Date: Feb 2005
Location: copenhagen, Denmark
Posts: 104
Default

Hmmm... dosn't work in my situation.. WHat Am I doing wrong..?


ActionScript Code:
import com.greensock.*; import com.greensock.plugins.*; TweenPlugin.activate([FramePlugin]); import flash.geom.Point; import com.evride.RemoveObject; var A:Array = new Array(con_mc.sb_01, con_mc.sb_02,con_mc.sb_03,con_mc.sb_04,con_mc.sb_05,con_mc.sb_06,con_mc.sb_07,con_mc.sb_08);     var i:int; for (i = 0; i < (A.length); i++)      {         A[i].addEventListener(MouseEvent.CLICK, onDown);         A[i].buttonMode = true;         A[i].useHandCursor = true; } function onDown(e:MouseEvent):void     {            var button:closeMC = new closeMC();         var stagePoint:Point = new Point(16, 110);         var targetPoint:Point = e.target.globalToLocal(stagePoint);         button.x = targetPoint.x;         button.y = targetPoint.y;               con_mc.setChildIndex(e.target as MovieClip,13);                 e.target.removeEventListener(MouseEvent.CLICK, onDown);         TweenLite.to(e.currentTarget, .5, {frame:05});                 e.target.addChild(button);         button.addEventListener(MouseEvent.CLICK, closeNow, false, 0, true);         } function closeNow(e:MouseEvent):void         {             TweenLite.to(e.target.parent, .5, {frame:1});             e.target.parent.addEventListener(MouseEvent.CLICK, onDown);             //e.target.parent.removeChild(e.target);             RemoveObject.remove(e.target);             trace(e.target.parent);//Null             }
pisosse is offline   Reply With Quote
Old 11-17-2010, 05:16 PM   #9
marmotronica
Registered User
 
Join Date: Nov 2010
Posts: 2
Default

Quote:
Originally Posted by evride View Post
So we get a lot of questions on here asking about removing movieclips and getting #2025 errors. I've written a class to help the budding Actionscripters to help them get past the errors.

Error 2025:
The error happens when you try to remove an object that isn't add to the stage or you do not specify the correct parent to remove the movieclip from.


This class does two things. It will get the correct parent object of the DisplayObject you are trying to remove. It will also silence the error so that your movie can continue to play.

This should not be a permanent fix, but actually a stepping stone used to help you learn actionscript 3 better and to prevent you from ripping your hair out.

how to use:
ActionScript Code:
import com.evride.RemoveObject; var mc:MovieClip = new MovieClip(); RemoveObject.remove(mc); //doesn't work and just traces an error (continues to play) addChild(mc); //adds the movieclip to the stage RemoveObject.remove(mc); //works fine, traces nothing.


here is the class. save it in your Actionscript 3 classes folder under com\evride
ActionScript Code:
////////////////////////////////////////// //   written by evride                  // //   Actionscript.org forums user       // //   found a problem? tell me there     // ////////////////////////////////////////// package com.evride{     public class RemoveObject{         public static function remove(obj:Object) {             if (obj) {                 if (obj is DisplayObject) {                     if(obj.parent){                         try {                             obj.parent.removeChild(obj);                         } catch (e:Error) {                             trace("Couldn't remove the object. It wasn't a child of a DisplayObject.");                         }                     }else{                         trace("Couldn't remove object. Parent property is null.");                     }                 } else {                     trace("Couldn't remove object. It is not a DisplayObject.");                 }             } else {                 trace("Couldn't remove the object. It was null or doesn't exist.");             }         }     } }
This fix itīs very easy and work 100%

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);

}
}
marmotronica is offline   Reply With Quote
Old 11-17-2010, 05:16 PM   #10
marmotronica
Registered User
 
Join Date: Nov 2010
Posts: 2
Default

This was my solution 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);

}
}
marmotronica is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
The ultimate solution for reusing packages among projects. jorgedbucaran ActionScript 3.0 2 12-26-2007 01:10 PM
Need alpha solution beckiehawk ActionScript 2.0 3 12-06-2007 01:08 AM
scrolled html page in flash / or another good solution kaziorvb Components 1 07-13-2007 07:07 PM
Adobe EOLAS solution not showing wmode tweaknee HTML and JavaScript 10 07-13-2006 09:32 PM
Hiding the solution fx2ooo ActionScript 2.0 0 06-30-2006 01:17 PM


All times are GMT. The time now is 12:19 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.