5 Replies Latest reply on Feb 26, 2007 1:47 PM by TigerArt

# Calculating X and Y of a child

When i add a child with ActionScript to a Canvas, HBox, or VBox the X and Y coordinates of the created child relative to it's parent always returns 0. How can I get the X and Y coordinates ??
• ###### 1. Re: Calculating X and Y of a child
It depends on when you are asking for them. If you add the child and then look at its x,y they will be zero because the framework hasn't had time to place them. If you listen for the updateComplete event on the container, you can then ask for the child's x,y and it should be correct.

Of course, with Canvas you need to specify the child's position, using a combination of x, y, left, right, top, and bottom.
• ###### 2. Re: Calculating X and Y of a child
Thank you very much for such quick answer. I'll try it !!
• ###### 3. Re: Calculating X and Y of a child
I'm trying to pass y coordiante to the label of an instanciated button. But it doesn't work. How can I do that ??

var enfant:Button = new Button();
enfant.width = 150;
pos.push(enfant.y);
enfant.x = enfant.mouseX;
enfant.y = enfant.mouseY;
}

private function onUpdateComplete( event:Event ){
event.target.label = event.target.y.toString();
}
• ###### 4. Re: Calculating X and Y of a child
Since you are using a Canvas, you must place its children. Canvas is just an open container allowing you the freedom to place your components anywhere you like. So when you create the Button, you have to give it x, y, width, height or else they will all default to zero.

Your statement: enfant.mouseX has no meaning at this point in the code because the mouse isn't involved there and the button has not be realized (drawn on the screen).

Using the updateComplete event is a good idea, though. When you get that event the button will have been drawn and placed so its Y coordinate will be valid.

If you do not want to position the children yourself, then use another container, not Canvas. Use VBox or HBox or Tile.
• ###### 5. Re: Calculating X and Y of a child
I corrected my code : I used ADDED event and event.currentTarget and it works fine.