ActiveDen

as3 check if movieclip exists?

140 posts
  • Has been a member for 4-5 years
  • Exclusive Author
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
za says

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?

3 years ago
755 posts
  • Has been a member for 3-4 years
  • Exclusive Author
  • Sold between 50 000 and 100 000 dollars
  • Elite Author
  • Bought between 1 and 9 items
  • Europe
  • Referred between 500 and 999 users
lydian says

Use stage.contains(...) or yourDisplayObjectContainer.contains(...) Use it like this;

if (stage.contains(myMc)) {
     // Do your work here
}
3 years ago
140 posts
  • Has been a member for 4-5 years
  • Exclusive Author
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
za says

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

3 years ago
755 posts
  • Has been a member for 3-4 years
  • Exclusive Author
  • Sold between 50 000 and 100 000 dollars
  • Elite Author
  • Bought between 1 and 9 items
  • Europe
  • Referred between 500 and 999 users
lydian says
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 :)

Pass the actual MC to the function, what you came up with is another solution.

3 years ago
58 posts
  • 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
agraddy says

This would be the way I would write it:

if (!contains(contactMC)) { //code; }

Although your method works too.

3 years ago
2408 posts Premium Scripts, Plugins and Themes
  • Has been a member for 5-6 years
  • Interviewed on the Envato Notes blog
  • Exclusive Author
  • Sold between 100 000 and 250 000 dollars
  • Elite Author
  • Bought between 10 and 49 items
  • Referred between 200 and 499 users
LucidStudios says
The right way is to check by getChildByName method as:
if (myContainer.getChildByName("myMC") == null) { //code; }

using contains method will generate error if it is undefined.

3 years ago
58 posts
  • 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
agraddy says

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

3 years ago
690 posts
  • Has been a member for 5-6 years
  • Exclusive Author
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
  • Canada
geoken says

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
}
3 years ago
3086 posts
  • 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
  • Beta Tester
  • Exclusive Author
  • Sold between 250 000 and 1 000 000 dollars
  • Elite Author
  • Bought between 10 and 49 items
  • Italy
  • Referred between 100 and 199 users
ParkerAndKent says
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”;

3 years ago
1 post
  • Has been a member for 3-4 years
alexzar5 says

try this…

try { removeChlld (mcName); } catch (e){}

3 years ago
by
by
by
by
by