Skip to main content
Known Participant
July 16, 2013
Question

removeChild. Where am I going wrong here?

  • July 16, 2013
  • 3 replies
  • 1647 views

I've tried so many things, but now I fold again. I'm trying to remove "holder" before I create it again.

function duplicateMov(clone:MovieClip, obj:MovieClip) {

           var bmd:BitmapData=new BitmapData(clone.width * SCALE, clone.height * SCALE, true, 0x00000000);

  var scaleMatrix:Matrix = new Matrix();

  scaleMatrix.scale(SCALE, SCALE);

          bmd.draw(clone, scaleMatrix);

     if (obj.holder != null && contains(obj.holder))

{

            obj.removeChild(holder);

}

 

 

            var holder:Bitmap = new Bitmap();

  holder.bitmapData = bmd

                    obj.addChild(holder);

 

 

 

}

This topic has been closed for replies.

3 replies

Inspiring
July 18, 2013

When dealing with bitmaps it is more efficient to reuse them - not create new instances. In your case just replacing BitmapData instance in the same Bitmap instance may be sufficient.

var holder:Bitmap = new Bitmap(null, "auto", true);

function duplicateMov(clone:MovieClip, obj:MovieClip):void

{

          var bmd:BitmapData = new BitmapData(clone.width * SCALE, clone.height * SCALE, true, 0x00000000);

          var scaleMatrix:Matrix = new Matrix();

          scaleMatrix.scale(SCALE, SCALE);

          bmd.draw(clone, scaleMatrix);

          holder.bitmapData = bmd

          obj.addChild(holder);

}

Amy Blankenship
Brainiac
July 18, 2013

If you're going to that route, you should keep a BitmapData made from each MC you might want to clone, then reuse that.

July 17, 2013

>>  if (obj.holder != null && contains(obj.holder))

You can't do both tests at one like this. What if obj.holder is null - then contains will fail. You need to split it up:

if(obj.holder != null){

     if(contains(obj.holder)){


Amy Blankenship
Brainiac
July 17, 2013

If obj.holder is null, the contains condition won't even run. So it's entirely legitimate (if logically flawed, because holder would be contained by obj, not this) to use the syntax the OP proposed.

Ned Murphy
Brainiac
July 16, 2013

What values do you get when you trace what you are testing in that conditional...

trace(obj.holder, contains(obj.holder));

if (obj.holder != null && contains(obj.holder)) 

{

You are likely going to have a problem declaring the holder object after you try referencing it.

Rhov23Author
Known Participant
July 16, 2013

"Parameter child must be non null."

But isn't that the point of the line?

if (obj.holder != null && contains(obj.holder)) -->  if (it) is not = to null and contains (it) then i can remove (it)

Everything besides the lines for removing the child works. The problem is that the obj:Movieclip keeps adding children instead of replacing the same one.

Amy Blankenship
Brainiac
July 16, 2013

You haven't really said what specifically is going wrong, so we can just ask questions.

Did you populate the obj.holder variable with your holder local variable when you called addChild with the local holder? If not, the next time you call removeChild on obj.holder, obj.holder will contain whatever was in it before (just removing the object doesn't reassign the variable). 

Note that I don't see where you did that. I also don't think that it could be anywhere else in the code (since the local holder only is in scope within duplicateMov). The only other place that you could be doing that is if you're watching for ADDED or ADDED_TO_STAGE on obj and you populate holder if you see a bitmap come on board.

Also note that if you add holder to obj then contains(obj.holder) will be false, because obj is what contains holder, not this.