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

If MovieClip exists how do I remove it?

Community Beginner ,
Nov 02, 2012 Nov 02, 2012

Copy link to clipboard

Copied

This should be simple, but for some reason I find myself struggling with this.  I need to check to see if a movieclip is instantiated.  If so I need it removed on MouseEvent.CLICK to load another mc.  Here is what I'm using thus far:

import flash.display.*

btnMenuOpt.addEventListener (MouseEvent.CLICK, loadMenuOpt);

function loadMenuOpt(event:MouseEvent):void{

          var mcmenuopt:mcMenuOpt = new mcMenuOpt();

          addChild(mcmenuopt);

          mcmenuopt.x=241.75;

          mcmenuopt.y=101.4;

}

btnWolfPack.addEventListener (MouseEvent.CLICK, loadWolfPack);

function loadWolfPack(event:MouseEvent):void{

          var mcwolfpack:mcWolfPack = new mcWolfPack();

          if (Boolean (this.getChildByName('mcMenuOpt'))) {

    removeChild(mcMenuOpt);}

          if (!Boolean(this.getChildByName('mcMenuOpt'))) {

              addChild(mcwolfpack);

          mcwolfpack.x=85.25;

          mcwolfpack.y=364.3;

          }

}

Whereas if mcMenuOpt is instantiated and btnWolfPack is clicked, mcMenuOpt goes bye-bye and mcWolfPack is instantiated.  Any ideas?  Thanks!

Oh yeah, it's also throwing this error at me:

Scene 1, Layer 'actions', Frame 1, Line 151067: Implicit coercion of a value of type Class to an unrelated type flash.display:DisplayObject.

TOPICS
ActionScript

Views

871

Translate

Translate

Report

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

correct answers 1 Correct answer

LEGEND , Nov 02, 2012 Nov 02, 2012

If you want to access the object in other functions then you need to declare it outside.  Try...

import flash.display.*

 
var mcmenuopt:mcMenuOpt;
var mcwolfpack:mcWolfPack;

 

btnMenuOpt.addEventListener (MouseEvent.CLICK, loadMenuOpt);
function loadMenuOpt(event:MouseEvent):void{
          mcmenuopt = new mcMenuOpt();
          addChild(mcmenuopt);
          mcmenuopt.x=241.75;
          mcmenuopt.y=101.4;
}


btnWolfPack.addEventListener (MouseEvent.CLICK, loadWolfPack);
function loadWolfPack(event:MouseEve

...

Votes

Translate

Translate
LEGEND ,
Nov 02, 2012 Nov 02, 2012

Copy link to clipboard

Copied

If you want to access the object in other functions then you need to declare it outside.  Try...

import flash.display.*

 
var mcmenuopt:mcMenuOpt;
var mcwolfpack:mcWolfPack;

 

btnMenuOpt.addEventListener (MouseEvent.CLICK, loadMenuOpt);
function loadMenuOpt(event:MouseEvent):void{
          mcmenuopt = new mcMenuOpt();
          addChild(mcmenuopt);
          mcmenuopt.x=241.75;
          mcmenuopt.y=101.4;
}


btnWolfPack.addEventListener (MouseEvent.CLICK, loadWolfPack);
function loadWolfPack(event:MouseEvent):void{
          mcwolfpack = new mcWolfPack();
          if (mcmenuopt) {
               removeChild(mcmenuopt);
               mcmenuopt = null;
          }
          addChild(mcwolfpack);
          mcwolfpack.x=85.25;
          mcwolfpack.y=364.3;
}

The error you get is because you are specifying the class name in the removeChild() call, not the instance.  When you create an object dynamically, if you don't assign it a name property, it does not have the variable name as its name, so the other check you did using getChildByName would fail as well.

Votes

Translate

Translate

Report

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
Community Beginner ,
Nov 26, 2012 Nov 26, 2012

Copy link to clipboard

Copied

Awesome, once again!  Thanks Ned!

Votes

Translate

Translate

Report

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 ,
Nov 26, 2012 Nov 26, 2012

Copy link to clipboard

Copied

LATEST

You're welcome

Votes

Translate

Translate

Report

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