Jack
Jack

Reputation: 57

Check if a child exists

I have two Scenes. In scene 1, there is a script which automatically adds a child using addChild(nameObject);, there is also a button go to the next scene.

When I come back from scene 2 to scene 1, the code generates another object. How do I make an AS3 script that checks if the child does already exist on the stage?

Upvotes: 0

Views: 494

Answers (1)

someOne
someOne

Reputation: 1675

You're most probably looking for the contains() method of a DisplayObjectContainer:

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.

if ( !contains(nameObject) ) {
    // the object is not a child of the container
    addChild(nameObject); 
}

Upvotes: 2

Related Questions