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

    Calculating X and Y of a child

    TigerArt Level 1
      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
          peterent Level 2
          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
            TigerArt Level 1
            Thank you very much for such quick answer. I'll try it !!
            • 3. Re: Calculating X and Y of a child
              TigerArt Level 1
              I'm trying to pass y coordiante to the label of an instanciated button. But it doesn't work. How can I do that ??


              private function addBtn(vb:Canvas):void{
              var enfant:Button = new Button();
              enfant.width = 150;
              vb.addChild(enfant);
              pos.push(enfant.y);
              enfant.x = enfant.mouseX;
              enfant.y = enfant.mouseY;
              enfant.addEventListener(MouseEvent.MOUSE_DOWN, onDraggableItemMouseDown, false,0.0,false);
              enfant.addEventListener(Event.UPDATE_COMPLETE, onUpdateComplete, false,0.0,false);
              }

              private function onUpdateComplete( event:Event ){
              event.target.label = event.target.y.toString();
              }
              • 4. Re: Calculating X and Y of a child
                peterent Level 2
                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
                  TigerArt Level 1
                  I corrected my code : I used ADDED event and event.currentTarget and it works fine.