asb asb - 5 months ago 24
Javascript Question

How do I detect a particular child on stage with createjs

How do I check if there is a particular child on stage and how do I check for a particular child A or B in createjs?

var A = new createjs.Bitmap('images.A');
var B = new createjs.Text('Hello','bold 12px Arial');
A.setTransform(100,100);
B.setTransform(100,150);
stage.addChild(A,B);
stage.update();

Answer

When you create the child element, give name to the child so that you can get the child back at a later stage by using the getChildByName('childname') method.

var A = new createjs.Bitmap('images.A');
var B = new createjs.Text('Hello','bold 12px Arial'); 
A.setTransform(100,100);
B.setTransform(100,150);
A.name = "somenameA";
B.name = "somenameB";
stage.addChild(A,B);
stage.update();

stage.getChildByName("somenameA");

This is assuming that your child are added in some module, and you want to get the child where the scope of your variable is no longer available. Otherwise, you can always use the variable to identify your child.

Comments