-
Notifications
You must be signed in to change notification settings - Fork 6
Navigating Items
Structure of Overlap2D scene is based on nested groups, meaning you have one root item that is a "group" or as we call it "composite", it may contain another simple items like images or animations, and it also may contain other groups. CompositeItem is the class we use for group and it extends FlxSpriteGroup, O2DSprite is the class we use for items and it extends FlxSprite.
A Nice feature of overlap2D is that you can set things like identifiers or tags to any item, and then later use that to navigate and find scene items you need.
If you have an item on scene that has id "foo" you need this code to retrieve it:
var foo: FlxSprite = sl.getRoot().getChild("foo").getFlxSprite();
If you have an item with id "foo" inside a composite called "bar":
var foo: FlxSprite = sl.getRoot().getChild("bar").
getChild("foo").getFlxSprite();
Besides identifiers you can also set several tags to an item, that is essentially an array of strings.
In order to check if item contains specific tag:
if(sl.getRoot().getChild("foo").getCoreData().hasTag("car")) {
// Yes foo is a car!
}
In order to get all items containing a tag (get all cars on the root element)
var result: Array<FlxSprite> = sl.getRoot().findChildrenByTag("cars");
Note: All this methods do not go recursively in depth, and only search in upper level of the group.