as3 quick q
- Started
- Last post
- 7 Responses
- scpgt
How can I check to see if a particular instance name exists in my movie?
Something like:
if(box1.stage) {
trace("exists");
}but this particular example produces a compiler error if box1 doesn't exist.. Is there a better way to go about it?
- scpgt0
if only twere that easy..
basically, there's several dozen dynamically generated movieclips and at a given point i need to check if a particular one exists on the stage..
you'd think it'd be straightforward but apparently not..
- forbes0
try
if(box1 =! null) {
trace("exists")'
}
- scpgt0
ahhh... nice try.. but same error:
1120: Access of undefined property box1.
- enjine0
there is a method to do this on every subclass of DisplayObjectContainer (i.e. Sprite, MovieClip, ...). From the language reference:
Determines whether the specified display object is a child of the DisplayObjectContainer instance or the instance itself. The search includes the entire display list including this DisplayObjectContainer instance. Grandchildren, great-grandchildren, and so on each return true.
Parameters
child:DisplayObject — The child object to test.Returns
Boolean — true if the child object is a child of the DisplayObjectContainer or the container itself; otherwise false.Example
The following example sets up a number of Sprite objects and adds some to the child list of others. (A Sprite object is a type of display object container.) The relationship between various objects is shown by calling the contains() method:import flash.display.Sprite;
var sprite1:Sprite = new Sprite();
var sprite2:Sprite = new Sprite();
var sprite3:Sprite = new Sprite();
var sprite4:Sprite = new Sprite();sprite1.addChild(sprite2);
sprite2.addChild(sprite3);trace(sprite1.contains(sprite1... // true
trace(sprite1.contains(sprite2... // true
trace(sprite1.contains(sprite3... // true
trace(sprite1.contains(sprite4... // false
- forbes0
var box1:BOX1 = new BOX1;
addChild(box1);if (box1 =! null) {
trace("exists");
}ok ive defined the box1 and added it to the stage.. try that
- scpgt0
Ta for the tips... by process of looking at those and then experimenting a bit, best option turned out to be
if (Boolean(getChildByName('box1... {
trace("exists");
} else {
trace("doesn't!");
}in case anyone's bothered :)
Ta again!