View Full Version : removeChild doesn't really remove it entirely?
cerupcat
10-24-2007, 12:43 AM
I have an bunch of sprites that are added to an array. The sprites have there own class, and the array is in the parent class. There's code where the sprites hit each other and interact.
In the sprite class (child) I want to remove the child from both the display and entirely when an event happens. When I do :
parent.removeChild(this)
The sprite is removed from the display, but still has it properties so other sprites that are still on stage are bouncing off an invisible sprite. How can I remove the invisible sprite entirely? Do I also need to remove it from the array? If so, how can I remove something from an array when it's the parent's array (not the child)?
Sly_cardinal
10-24-2007, 12:52 AM
"Do I also need to remove it from the array? If so, how can I remove something from an array when it's the parent's array (not the child)?"
If there is a separate array in the parent that holds a reference to the child object then yes you would need to remove it from that array as well.
You could remove the child from the array by creating a function on the parent that will remove a particular item from the array. Call this function from the child object, passing a reference to itself as a parameter so the parent can find it in the array.
You might need to do some trickery and cast the child's parent property to the parent's class so you can call the function.
cerupcat
10-24-2007, 01:03 AM
Ok, I created the function in the parent clip.
public function removeSquare(square){
removeChild.(square);
var index = getChildIndex(square);
balls.splice( index, 1 );
}
But I'm not able to call it in the child's class =\ Not sure how you would do that.
Sly_cardinal
10-24-2007, 01:24 AM
Cast the 'parent' property to the parent's class and call the removeSquare function:
ParentClass(parent).removeSquare(this);
(Make sure that you put in the actual class name of the parent - ParentClass is just an example)
Make sure that the child doesn't remove itself from the parent's display list if the parent does that in it's function - you will run into problems otherwise.
cerupcat
10-24-2007, 01:28 AM
Ok thanks.
I tried this and it works, but I guess maybe this isn't what I want. How do I totally remove the sprite? I got it to remove itself from the array and also removed it's display, but all it's properties are still there. I can tell because it's still creating sound.
Is there an easy way to totally remove the sprite so it stops any calculations that it once had (like sound, bounce, x, y, etc.)?
Sly_cardinal
10-24-2007, 01:32 AM
That depends on your code. If you are using the 'balls' array to caulculate collisions then removing it from there (as you do in your removeSquare function) should fix the problem - the issue was you had two references to the MovieClip in independent lits: one in the display list, one in the array used to calculate collissions. Removing the movieclip from one list wouldn't affect it in the other, you must remove it from both.
Actually, having reviewed that function I have found a little issue: move the removeChild call after the 'var index...' line. Otherwise you will be trying to find the index of a child that you have already removed from the display list.
So it should be something like this:
public function removeSquare(square){
var index = getChildIndex(square);
balls.splice( index, 1 );
removeChild(square);
}
cerupcat
10-24-2007, 01:40 AM
OK, so basically I have to remove all eventlisteners and every property the square had? There isn't one method to totally just remove it?
For example, right now the ball makes a sound when it hits a certain X axis. When I drag the ball farther than that axis, I want to delete it. Right now, it's been removed from the array (and no longer hit tests) and was removed from the displaylist and can't be seen (removeChild), but it is still making sound since it's beyond the axis. If I could just remove it entirely then it would have no sound or other calculations that will waste memory + CPU.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.