i’m very new to as3 and i’m dynamically creating mc’s but i have no idea how to check if the mc exists already on the stage so i am creating duplicates of the same clip. i’ve tried several methods off of the kirupa forums and nothing seems to be working. anyone have any ideas on this?
- Has been a member for 4-5 years
- Exclusive Author
- Sold between 1 000 and 5 000 dollars
- Bought between 10 and 49 items
- Has been a member for 3-4 years
- Item was Featured
- Author was Featured
- Exclusive Author
- Sold between 50 000 and 100 000 dollars
- Bought between 1 and 9 items
- Europe
- Referred between 500 and 999 users
Use stage.contains(...) or yourDisplayObjectContainer.contains(...) Use it like this;
if (stage.contains(myMc)) {
// Do your work here
}
- Has been a member for 4-5 years
- Exclusive Author
- Sold between 1 000 and 5 000 dollars
- Bought between 10 and 49 items
for some reason that wasn’t working for me and i ended up using
if (this.getChildByName(contactMC) == null) { //code; }
code be other issues with my code though as i’m a newb 
- Has been a member for 3-4 years
- Item was Featured
- Author was Featured
- Exclusive Author
- Sold between 50 000 and 100 000 dollars
- Bought between 1 and 9 items
- Europe
- Referred between 500 and 999 users
for some reason that wasn’t working for me and i ended up usingif (this.getChildByName(contactMC) == null) { //code; }
code be other issues with my code though as i’m a newb
Pass the actual MC to the function, what you came up with is another solution.
- Has been a member for 3-4 years
- Sold between 100 and 1 000 dollars
- Bought between 1 and 9 items
- Referred between 1 and 9 users
This would be the way I would write it:
if (!contains(contactMC)) { //code; }
Although your method works too.
- Has been a member for 5-6 years
- Interviewed on the Envato Notes blog
- Item was Featured
- Author was Featured
- Exclusive Author
- Sold between 100 000 and 250 000 dollars
- Bought between 10 and 49 items
- Referred between 200 and 499 users
if (myContainer.getChildByName("myMC") == null) { //code; }
using contains method will generate error if it is undefined.
- Has been a member for 3-4 years
- Sold between 100 and 1 000 dollars
- Bought between 1 and 9 items
- Referred between 1 and 9 users
Saafi is right…ignore my advice. You only want to use contain if you are checking if an object that is already defined is on the display list.
I think my brain saw “check if movieclip exists” and thought “exists on the display list”...sorry for directing you wrong.
Glad you were able to get to the right code on your own 
- Has been a member for 5-6 years
- Author was Featured
- Exclusive Author
- Sold between 1 000 and 5 000 dollars
- Bought between 10 and 49 items
- Canada
You still have to use getChildByName (or getChildAt if you know the depth) because the dynamically created function wont have an instance name that can be globally accessed.
For example, this code would fail;
function create():void{
var myMC:MovieClip = new MovieClip
addChild(myMC)
}
function move():void{
myMC.x += 10
}
- Has been a member for 3-4 years
- Author had a File in an Envato Bundle
- Interviewed on the Envato Notes blog
- Author had a Free File of the Month
- Item was Featured
- Author was Featured
- Beta Tester
- Exclusive Author
- Sold between 250 000 and 1 000 000 dollars
- Bought between 10 and 49 items
- Italy
- Referred between 100 and 199 users
You still have to use getChildByName (or getChildAt if you know the depth) because the dynamically created function wont have an instance name that can be globally accessed.For example, this code would fail;
function create():void{ var myMC:MovieClip = new MovieClip addChild(myMC) } function move():void{ myMC.x += 10 }
This fails because you are creating an instance of MovieClip in a function scope and then you try to access that instance from another function scope.
Then for dynamically created instances it’s quite easy giving them an instance name to use with getChildByName method:
var myClip:MovieClip = new MovieClip();
myClip.name = “myInstanceName”;
