0

I am a baffled noob here. I have the following code:

var mC:mc = new mc();

I do NOT instantiate mC at all with an addChild(mC);

But, later in the code, I have a loop using onEnterFrame and in this loop I have the following trace function:

if(mC){
    trace("mC is here");
}

This returns "mC is here" in the output window. HUH???

The problem is that I want to use this 'if' statement to removeChild(mC); [I will be adding it in the code later with an addChild(mC); based on certain stuff that happens] but it keeps throwing dang "ERROR child of the caller" messages even with the 'if' condition...

WHAT am I doing wrong? I did not know declaring variables would add them to the stage/display list, I thought you needed an addChild(); statement. Am I smoking something I shouldn't be?

Thanks in advance, ~Frustrated Inc.

coderJoker
  • 26
  • 5
  • Doing "var mC:mc = new mc();" is called "instantiating", it is the process of creating an Object. What you call instantiating "addChild" has nothing to do with that process. – BotMaster Dec 12 '15 at 15:04

3 Answers3

1

When you new up an object it exists in memory, even if you have not added it to the stage. That is why when you check if mC exists, it returns true. You want to check if it exists on the stage. Something like:

var mc:MovieClip = new MovieClip();
mc.name = "test";
if (this.getChildByName("test") != null) {
  trace("mc is on stage");
}

I have not used Flash for a long time, so I did not test this code. Hopefully it works.

Fraser Crosbie
  • 1,672
  • 1
  • 12
  • 21
  • Thank you Fraser. OK I get that '!= null' means 'not equals null' but should I be using this same approach every time I want to build an 'if' statement regarding mC? So, if I want to move it on the stage, remove it from the stage, etc. I need to use 'if(this.getChildByName() != null' every time? It seems that other MovieClips I add don't need this code but this one does... ~Confused – coderJoker Dec 12 '15 at 00:13
  • @coderJoker Normally you will add the MovieClip to the stage immediately after instantiating it. In most situations you can assume that the MovieClip will be on the stage. Only in situations when you need to remove the MovieClip from the stage for some reason or another, would you want to check for it. When you are running into runtime errors, then you would add the if statement to prevent the error from occurring again. – Fraser Crosbie Dec 12 '15 at 01:22
  • Now it's throwing Error #1006: getChildByName is not a function... I just don't get what I'm doing wrong. There must be something else throwing a spanner in the works... – coderJoker Dec 12 '15 at 01:51
  • Maybe you can find your answer here: http://stackoverflow.com/questions/7280203/how-do-i-access-a-movieclip-on-the-stage-using-as3-class – Fraser Crosbie Dec 12 '15 at 01:53
  • Thank you Fraser. I clearly have more to learn about the display, the stage and addresses. I removed 'this' from your example and it works now. I don't know why, and I need to learn why before I can progress as a coder. Knowing what question to ask is as important as getting answers to questions. Thank you for all of your help! CJ – coderJoker Dec 12 '15 at 02:06
  • That last statement is a pure opinion and not a fact. It is also completely wrong and illogical and only shows a total lack of serious and real knowledge. By "IPhone" I guess he meant Ios and Ios and Flash have not much to do together since they don't do the same thing. Ios does not impact other technology in a negative way and in fact a ton of other technology are flourishing. Why would Flash be impacted in anyway by Ios is a mistery only in the mind of the last comment's author. – BotMaster Dec 12 '15 at 03:53
1

In your code, you just control whether your variable is null or not.

You can use contains method on the display object you are trying to add to.

If you are adding mC to some sprite named container, you can simply check whether it exists in that container with:

if (!container.contains(mC))
     container.addChild(mC);

Edit: The safer method to control whether a movieclip is on the stage is to control its stage value.

if (mC.stage) {
     mC.parent.removeChild(mC); // this is how you remove, if you simply want to check existence, don't remove it
}

It has to have a stage value if you added the movieclip to the stage or a container that is added to stage.

Hope it is clearer this way.

berkayk
  • 2,376
  • 3
  • 21
  • 26
  • Thank you berkayk! I will learn this technique next, it looks very useful. – coderJoker Dec 12 '15 at 02:08
  • Voted down. This is a misguided answer. The contains method is not meant for that purpose and does not deliver the correct result. The contains methods is a check to see if a DisplayObject is ANYWHERE within the entire display list of the caller. That means it will return true even if the checked DisplayObject is 1, 3, or 30 levels down the display list. So using that and in order to use a removeChild is simply wrong since that removeChild can still throw error since the contains does not guaranty that the DisplayObject is a child of the caller. – BotMaster Dec 12 '15 at 03:46
  • My answer is not much different then yours @BotMaster Yes, it is safer to check for parent before looking for contains if you don't know where you put your movieclip. In your case, you can add mC to another container and you may not see in the the stage because the container you added is not added to stage, but mc.parent returns not null. It is a preference of the developer, since I know where I put that movieclip I prefer using contains. Your answer may also behave abnormal if you didn't add the parent to the stage. – berkayk Dec 12 '15 at 09:02
  • My answer would work even when parent is not in a display list since all it does is check for parent and then remove from that parent. If the whole thing is on the stage or not won't stop it from working. The contains method is not only not meant to check if a child is in the direct parent display list, it is also by definition slow as it has to go through all display list of all descendants. Let's do a perf comparison on a complex scene within a Nk (let's start at 5000) loop to see which system is gonna be faster. Using contains is gonna be far behind + doesn't guaranty anything. – BotMaster Dec 12 '15 at 15:01
  • But to be fair I posted that comment mostly in the interest of @codejoker. Let's say he uses your method in a complex scene with a lot of movies and layers, the contains method (since it's so slow) might start to slow down everything and produce lag and he will have a hard time figuring where that comes from. So now at least he knows that contains method does not guaranty an object is a direct child of a parent and is very slow so he can make his own choice. – BotMaster Dec 12 '15 at 15:08
  • Ok you win :) @BotMaster – berkayk Dec 13 '15 at 15:15
  • 1
    the stage check is better, removed the vote down. Checking parent still would be more to the point. – BotMaster Dec 14 '15 at 11:48
1

Complex objects in AS3 (that means anything that is not a string or a number) have a default value of null. WHen evaluated that default value of null equals false:

var mymc:MovieClip;//that MC is NOT instantiated yet so it has a default value of null
if(mymc)
{
    //mymc is null so this evaluates to false
    //and this statement DOES NOT execute

Now when a complex object is instantiated and exist its value would now evaluates to true

var mymc:MovieClip = new MovieClip();//that MC IS instantiated 
if(mymc)
{
    //mymc exits so this evaluates to true and this statement EXECUTE
    //notice that "!= null" is not necessary

Now your problem has to do with display list. A DisplayObject has a parent property that is null when that object is not added to a display list, and that property point to the parent when that object is added to a display list:

var mc:MovieClip = new MovieClip()
trace(mc.parent);//this is null
addChild(mc);
trace(mc.parent);//this is not null anymore and points to the parent

SO what you mean to do is:

if(mC.parent){//this means mC has a parent and can be removed from it
    trace("mC is here");        
}
BotMaster
  • 2,233
  • 1
  • 13
  • 16
  • +1, I was thinking to put an answer about `mc.parent` with the same example, you beat me to it ;) I think that the best and easiest way is to verify the object's parent. – akmozo Dec 12 '15 at 10:29