View Full Version : AS3: How to check if a movieclip already exists?
DavidOrtiz
November 21st, 2007, 02:33 PM
In AS2, I'd create a movieclip and give it an instance name. Then all I'd have to do for example is, check if _root["myinstance"+i] == undefined
but in AS3, if i'm dynamically creating 10 movieclips, how do i check to see if it already exists?
sorry if the question is dumb.
453.0
November 21st, 2007, 03:52 PM
trace them ?
DavidOrtiz
November 21st, 2007, 06:12 PM
i need to be able to do some kind of conditional test like
if(movieclip x inside of movieclip container does NOT exists){
create moveclip x inside of movieclip container
}
Karinger
November 21st, 2007, 06:30 PM
Would this do the trick?:
container.getChildByName("myDynamicClip") == null
If you have the child's name that you want to find you can use the getChildByName function, this will return the index the child is at if it exists, or null if the child does not exist in the container.
scetian
December 6th, 2007, 06:57 AM
If you have added them on stage or on any Movieclip then you can do this...
this.contains(nameoftheMovieClip) this will return true if it movieclip is there on the stage or MovieClip....
Dazzer
December 6th, 2007, 07:36 AM
scetian: does not work. You must give it a reference to a MovieClip, not the name of the MovieClip
hobbbz
December 7th, 2007, 05:11 PM
if (myMovieClip){
//stuff
}
Dazzer
December 7th, 2007, 11:20 PM
Karinger and Scetian are on the right track with this.
Just my 2p.
Gathan
December 7th, 2007, 11:38 PM
You would use the getChildByName, wrap it in the Boolean class and then you can put it in a if statement like
if (Boolean(this.getChildByName('mcname'))) {
//clip exists don't do anything
if (!Boolean(this.getChildByName('mcname'))) {
//clip doesn't exist create it
}
McGuffin
December 8th, 2007, 12:21 AM
if (!DisplayObjectContainer(parent).contains(parent.g etChildByName('myMC'))) {
var s:Sprite = new Sprite();
s.name = "myMC";
parent.addChild(s);
}
flashTrev
August 9th, 2009, 06:53 AM
Firstly a big thanks to David for posting the question and to Gathan for supplying an answer that worked for me, cheers.
I've expanded on it a little but am confused as to why it says the movieClip im looking for doesn't exist when I can see it in my 'insert a Target path list' and its visible on the screen. Let me expand a bit....
here is my code:
if (Boolean(this.mainArea_mc.infoArea_mc.getChildByNa me('coolText_mc')))
{
trace("clip exists");
}
if (!Boolean(this.mainArea_mc.infoArea_mc.getChildByN ame('coolText_mc')))
{
trace("clip doesn't exist");
}
this works fine if in movieClip 'infoArea_mc', 'coolText_mc' is on the stage for all the frames (but alpha '0' for the ones not required).
But if it is only on the stage on frame 4, and I dont run this check till I have checked that frame 4 is displaying on the screen (and I can physically see it), when I run this check it still gives "clip doesn't exist"???
any ideas why?
thanks
Wocker
November 22nd, 2009, 03:42 AM
Another method is to check if an object (movieClip) is IN another object
if (targetClip in parentClip) {
trace('clip found:'+targetClip.name+' in '+parentClip.name);
}
This code WILL break if the parentClip you provide isn't available.
You can always add an extra check to prevent that.
TheCanadian
November 22nd, 2009, 04:14 AM
try..catch would work for this as well.
minc
February 2nd, 2010, 11:39 AM
Yup. Worked for me as well.. Thanks Gathan...
mlo
March 4th, 2010, 05:34 AM
hi to everybody here, I´m trying to determine by as3 code if a son movieclip exists on stage or not. For that I made a .fla, in its main timeline I created a mc named ddd, and inside of it another movieclip named vvv. I tried all alternatives in this forum page without result.
here´s all I tried without obtaining a trace saying that my son vvv mc really is on stage!:
/*
if (ddd.getChildByName("vvv")!=null) {
trace("clip exists don't do anything");
}
*/
/*
if (ddd.contains(vvv)) {
trace("clip exists don't do anything");
}
*/
/*
if (vvv in ddd) {
trace("clip exists don't do anything");
}
*/
/*
if (Boolean(this.ddd.getChildByName('vvv'))) {
trace("clip exists don't do anything");
}
if (! Boolean(this.ddd.getChildByName('vvv'))) {
trace("clip doesn´t exists don't do anything");
}*/
if (DisplayObjectContainer(this.ddd).contains(this.dd d.getChildByName('vvv'))) {
trace("clip exists don't do anything");
}
if anybody can help me, tnks a lot and regards from Spain...
KrisRoe
March 10th, 2010, 01:16 PM
Hey I stumbled upon this thread searching for a solution to this same problem, dunno if its just outdated or something but trying several stuff to check if a mc is on stage I mixed up two solutions proposed by hobbbz and gathan... The easiest way I found to do this is simply check for the movieclip like hobbbz but wrapping it in a bolean class so it is a shorter option
if (Boolean(dynamicMovieClip)) {
//whatevs!
}
Dunno if this will ever help someone but it did the charm for me thx!!! :grin:
Loran_M
April 13th, 2010, 05:24 AM
@KrisRoe: If it works, you have a beer from me. ;d
_kp
April 13th, 2010, 05:46 AM
if (Boolean(dynamicMovieClip)) {
//whatevs!
}
There's a difference between a movieclip being in the display list, being a child of another mc (that doesn't need to be on display list) and existing at all.
The code above is the same as using
if( dynamicMovieClip )
This simply check if the variable is null.
Something like == null can be dropped, since a returned null is equal to false in those statements (it may still improves readability) - so there is really no need to cast it to Boolean by hand.
if( dynamicMovieClip.stage )
checks if the clip has a stage property set, which can be used to see if the movieclip is in the display list.
chronoshift
September 30th, 2010, 09:24 PM
If you have added them on stage or on any Movieclip then you can do this...
this.contains(nameoftheMovieClip) this will return true if it movieclip is there on the stage or MovieClip....
Thank you very much. that's just what I was looking for.
PRGM2
January 20th, 2011, 05:50 PM
This one saved me a lot of time and mental anguish.:thumb:
If you have added them on stage or on any Movieclip then you can do this...
this.contains(nameoftheMovieClip) this will return true if it movieclip is there on the stage or MovieClip....
evolutionxbox
May 30th, 2011, 10:34 AM
I am trying to check if a child movieclip exists but when I run the code you lot have suggested I get a "TypeError: Error #2007: Parameter child must be non-null" error... but the point is it may not exist so why does it error when it doesn't?
It's checking to see if it's not there but when it isn't there it throws an error instead of 'false'.
Ali V.
January 2nd, 2012, 08:43 AM
This may be sometimes very confusing.
It is important whether you have initialized your MC or not.
var MC:Movieclip; --> not initialized yet
var MC:Movieclip = new MC_Object(); --> initialized
1. if (MC): this statement does not test whether MC exists on stage, this tests if MC has been initialized.
2. if (contains(MC)): tests whether MC exists on stage. Note that MC should be initialized first, or you'll get an error.
So, to check whether a MC has been added to stage, you can use "contains" keyword, but your MC should be initialized. Otherwise use if(MC) statement to check if object is initialized.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.