Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to gets DO's children recursively (optional). #709

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions starling/src/starling/display/DisplayObjectContainer.as
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,24 @@ package starling.display
throw new RangeError("Invalid child index");
}

/** Returns a child object with a certain name (non-recursively). */
public function getChildByName(name:String):DisplayObject
/** Returns a child object with a certain name. There is option to search
* recursively. */
public function getChildByName(name:String, searchInChildren:Boolean=false):DisplayObject
{
var numChildren:int = mChildren.length;
for (var i:int=0; i<numChildren; ++i)
if (mChildren[i].name == name) return mChildren[i];
if (searchInChildren)
{
var childPath:Array = name.split(".");
var target:DisplayObjectContainer = this;
for each (var childName:String in childPath)
{
target = target.getChildByName(childName) as DisplayObjectContainer;
}
return target;
} else {
var numChildren:int = mChildren.length;
for (var i:int=0; i<numChildren; ++i)
if (mChildren[i].name == name) return mChildren[i];
}

return null;
}
Expand Down
36 changes: 35 additions & 1 deletion tests/src/tests/display/DisplayObjectContainerTest.as
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,41 @@ package tests.display
child2.name = "child3";
Assert.assertEquals(child2, parent.getChildByName("child3"));
}


[Test]
public function testGetChildByNameRecursively():void
{
var parent:Sprite = new Sprite();
var child1:Sprite = new Sprite();
child1.name = "child1";

var child2:Sprite = new Sprite();
child2.name = "child2";

var child3:Sprite = new Sprite();
child3.name = "child3";

parent.addChild(child1);
child1.addChild(child2);
child2.addChild(child3);

Assert.assertEquals(child1, parent.getChildByName("child1", true));
Assert.assertEquals(child2, parent.getChildByName("child1.child2", true));
Assert.assertEquals(child3, parent.getChildByName("child1.child2.child3", true));
Assert.assertNull(parent.getChildByName("child1.child2"));
}

[Test(expects="TypeError")]
public function testGetChildByNameRecursivelyWithError():void
{
var parent:Sprite = new Sprite();
var child1:Sprite = new Sprite();
child1.name = "child1";

parent.addChild(child1);
parent.getChildByName("child1.child2.child3", true);
}

[Test]
public function testSetChildIndex():void
{
Expand Down