as3 check if movieclip exists?


#1

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?


#2

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

if (stage.contains(myMc)) {
     // Do your work here
}

#3

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


#4
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 :slight_smile:

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


#5

This would be the way I would write it:

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

Although your method works too.


#6

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.


#7

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


#8

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
}

#9
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”;


#10

try this…

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


#11

there is multple things you have to check, 1) if it exists 2) if it is in the display list of the movieclip you want. check this:

http://pastie.org/395812

And don’t use names of movie clips to find stuff, that’s AS2 crap :stuck_out_tongue: Reference the movie clip itself.


#12

Don’t you just love it when noobs turn up on the forums answering months-old threads in their first post :slight_smile:
That’s the second one in a week :stuck_out_tongue:


#13

what is this doing

if(mc){
//on stage
} else {
// not on stage…
}

i am using this, and works fine


#14

well, this works for me…

try{
trace(getChildIndex(DisplayObject));
}catch(e:Error){
//create it
}

Thanks,


#15
well, this works for me...

try{
trace(getChildIndex(DisplayObject));
}catch(e:Error){
//create it
}

Thanks,

Wow … “deja vu all over again”:http://activeden.net/forums/thread/as3-check-if-movieclip-exists/8272?page=2#86320


#16

this is wrong,
if(mc){ //on stage } else { // not on stage… }

this is right
getChildIndex(DisplayObject);


#17
this is wrong, if(mc){ //on stage } else { // not on stage.. }

this is right
getChildIndex(DisplayObject);

Now we have people coming back and correcting their own 6-month old mistake (which was already irrelevant and 6 months late when first posted)! This is starting to feel like the Twilight Zone :open_mouth:


#18
this is wrong, if(mc){ //on stage } else { // not on stage.. }

this is right
getChildIndex(DisplayObject);

Now we have people coming back and correcting their own 6-month old mistake (which was already irrelevant and 6 months late when first posted)! This is starting to feel like the Twilight Zone :open_mouth:

answer came 6 monts later and the main is not changed, there are people coming from google, they deserve answer.


#19

thanks a lot guys, very useful tip!