PDA

View Full Version : adding and removing childs - how to do it safe



LeonardoZimbres
September 13th, 2007, 01:54 PM
Hello Kirupa Forum,,

I got a problem with something, hope if someone know the awser.

I did a site navigation based on frames. When the site is on frame1, it addChild of a movieclip called 'mc'. But when I go to frame 2, I dont need mc anymore, so I call removeChild to remove the undesired mc. But when I go from another frame and step in frame 2 again, the mc does no exist and the code halts on the removeChild line. Someone have a good idea?

buginajar
September 13th, 2007, 01:57 PM
Hello Kirupa Forum,,

I got a problem with something, hope if someone know the awser.

I did a site navigation based on frames. When the site is on frame1, it addChild of a movieclip called 'mc'. But when I go to frame 2, I dont need mc anymore, so I call removeChild to remove the undesired mc. But when I go from another frame and step in frame 2 again, the mc does no exist and the code halts on the removeChild line. Someone have a good idea?

try this, it will only remove the child if it exists on the display list:


if (contains(myMovieClip_mc)) removeChild(myMovieClip_mc);

LeonardoZimbres
September 13th, 2007, 03:31 PM
flash keep halting, he says:
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/contains()
at indexo_fla::z_sitesections_2/resetSite()
at indexo_fla::z_sitesections_2/indexo_fla::frame1()

senocular
September 13th, 2007, 04:53 PM
contains also accounts for grand children and objects further in the hierarchy.

What you will want to do is


if (myMovieClip_mc && myMovieClip_mc.parent == this) {
removeChild(myMovieClip_mc);
}

soulwire
September 13th, 2007, 04:53 PM
Try assigning you created mc to a top level variable, then just check if that exists and if so removeChild

soulwire
September 13th, 2007, 04:54 PM
Lol, beat me to it Sen. Do what he says ^ ;)

LeonardoZimbres
September 13th, 2007, 05:06 PM
Wow, the great Senocular from the hardcore isometric tutorials... Thanks for the help! I did not got yet... but works!

Strange piece of code...
myMovieClip_mc && myMovieClip_mc.parent == this

LeonardoZimbres
September 13th, 2007, 05:12 PM
tell me, what this means?
myMovieClip_mc && myMovieClip_mc.parent == this

looks like that Macgiver(from that old tv show) wrothe this code.

McGuffin
September 13th, 2007, 05:16 PM
tell me, what this means?
myMovieClip_mc && myMovieClip_mc.parent == this

looks like that Macgiver(from that old tv show) wrothe this code.

It checks to see if myMovieClip_mc is non-null, which basically means it exists, and then checks to see if myMovieClip_mc's parent is the current movieclip. If both these things check out, you can use removeChild safely.

soulwire
September 13th, 2007, 05:19 PM
And everyone lived happily every after