Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

removeChild. Where am I going wrong here?

New Here ,
Jul 16, 2013 Jul 16, 2013

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

 

 

 

}

TOPICS
ActionScript
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 16, 2013 Jul 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 16, 2013 Jul 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 16, 2013 Jul 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 16, 2013 Jul 16, 2013

What is going wrong is that I'm pretty new to the whole coding thing.

I'm trying to update a clone of some movieclip to some other movieclips. This works. The problem is that duplicateMov functions keeps adding movieclips to the stage instead of replacing them. I need to -remove and add "holder" - every time, except the first time I add it.

This is on stage:

function toggleLayerMenu(event:MouseEvent):void{

          if(layerMenu.visible == true){

                    layerMenu.visible = false;

 

          }else{

                    layerMenu.visible = true;

          layerMenu.duplicateMov(LHolder.L1Holder, Box(layerMenu.getChildByName("layer1")).getChildByName("L0Dup"));

          layerMenu.duplicateMov(LHolder.L2Holder, Box(layerMenu.getChildByName("layer2")).getChildByName("L0Dup"));

          layerMenu.duplicateMov(LHolder.L3Holder, Box(layerMenu.getChildByName("layer3")).getChildByName("L0Dup"));

          layerMenu.duplicateMov(LHolder.L4Holder, Box(layerMenu.getChildByName("layer4")).getChildByName("L0Dup"));

          layerMenu.duplicateMov(LHolder.L5Holder, Box(layerMenu.getChildByName("layer5")).getChildByName("L0Dup"));

          layerMenu.duplicateMov(LHolder.L6Holder, Box(layerMenu.getChildByName("layer6")).getChildByName("L0Dup"));

          layerMenu.duplicateMov(LHolder.L7Holder, Box(layerMenu.getChildByName("layer7")).getChildByName("L0Dup"));

          layerMenu.duplicateMov(LHolder.L8Holder, Box(layerMenu.getChildByName("layer8")).getChildByName("L0Dup"));

          layerMenu.duplicateMov(LHolder.L9Holder, Box(layerMenu.getChildByName("layer9")).getChildByName("L0Dup"));

          layerMenu.duplicateMov(LHolder.L10Holder,Box(layerMenu.getChildByName("layer10")).getChildByName("L0Dup"));

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 16, 2013 Jul 16, 2013

keeps adding movieclips to the stage

edit: keep adding new instances of "holder" to the movieclip.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 16, 2013 Jul 16, 2013

Try something like

var holder:Bitmap = new Bitmap();

holder.bitmapData = bmd

obj.holder = holder;

in the Class you're using for obj:

protected var _holder:Bitmap;

public function get holder():Bitmap {

     return _holder;

}

public function set holder(value:Bitmap):void {

     if (value !== _holder) {

          if (contains(_holder) {

               removeChild(_holder);

          }

          _holder = value;

          if (_holder) {

               addChild(_holder);

          }

     }

}

This puts the responsibility for managing the children of obj firmly with obj. And since obj necessarily has the most knowledge about its own children, this is exactly who should have that responsibility.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 16, 2013 Jul 16, 2013

I'll try and see what I can do. Thanks.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 16, 2013 Jul 16, 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)){


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 17, 2013 Jul 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 18, 2013 Jul 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);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 18, 2013 Jul 18, 2013
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines