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?
Use stage.contains(…) or yourDisplayObjectContainer.contains(…)
Use it like this;
if (stage.contains(myMc)) { // Do your work here }
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
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.
This would be the way I would write it:
if (!contains(contactMC)) { //code; }
Although your method works too.
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.
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
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 }
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”;
try this…
try {
removeChlld (mcName);
} catch (e){}
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:
And don’t use names of movie clips to find stuff, that’s AS2 crap Reference the movie clip itself.
Don’t you just love it when noobs turn up on the forums answering months-old threads in their first post
That’s the second one in a week
what is this doing
if(mc){
//on stage
} else {
// not on stage…
}
i am using this, and works fine
well, this works for me…
try{
trace(getChildIndex(DisplayObject));
}catch(e:Error){
//create it
}
Thanks,
well, this works for me...try{
trace(getChildIndex(DisplayObject));
}catch(e:Error){
//create it
}Thanks,
Wow … “deja vu all over again”:Buy Plugins & Code from CodeCanyon
this is wrong,
if(mc){ //on stage } else { // not on stage… }
this is right
getChildIndex(DisplayObject);
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
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
answer came 6 monts later and the main is not changed, there are people coming from google, they deserve answer.
thanks a lot guys, very useful tip!