3 Replies Latest reply on Sep 19, 2007 9:45 PM by doug777

    Find position of display object

    doug777 Level 1
      Is it possible to find the x and y position of a display object in an automatically created layout?

      I want to place a vertical rule on a grid container at the position of a particular grid item.

      The position of this grid item depends on the content of the grid which is dynamically created and cannot be known in advance.

      Does anyone have any ideas as to how this might be done?

      Doug
        • 1. Re: Find position of display object
          Daniel_Summers
          You're probably going to want to use getBounds()

          This is the example Adobe provides:

          --------------------------------------------------

          var container:Sprite = new Sprite();
          container.x = 100;
          container.y = 100;
          this.addChild(container);
          var contents:Shape = new Shape();
          contents.graphics.drawCircle(0,0,100);
          container.addChild(contents);
          trace(contents.getBounds(container));
          // (x=-100, y=-100, w=200, h=200)
          trace(contents.getBounds(this));
          // (x=0, y=0, w=200, h=200)

          ------------------------------------------------------

          For an app I'm building I had to also take into account the vert. & hor. scrolling:

          ------------------------------------------------------

          var bounds:Rectangle = this.getBounds( (this.parentApplication as Application) );

          individualSortPopup = new IndividualsSortPopup();
          individualSortPopup.name = "individualSortPopup";
          individualSortPopup.x = bounds.x + (parentApplication as Application).horizontalScrollPosition;
          individualSortPopup.y = bounds.y + (parentApplication as Application).verticalScrollPosition;
          (parentApplication as Application).addChild( individualSortPopup );

          ------------------------------------------------------
          • 2. Re: Find position of display object
            Daniel_Summers Level 1
            Also found another solution in looking this one up -- prob help me too.

            Use localToGlobal()

            ----------------------------------------------------------

            import flash.display.Sprite;
            import flash.events.MouseEvent;
            import flash.geom.Point;

            var square:Sprite = new Sprite();
            square.graphics.beginFill(0xFFCC00);
            square.graphics.drawRect(0, 0, 100, 100);
            square.x = 100;
            square.y = 200;

            addChild(square);

            square.addEventListener(MouseEvent.CLICK, traceCoordinates)

            function traceCoordinates(event:MouseEvent):void {
            var clickPoint:Point = new Point(square.mouseX, square.mouseY);
            trace("display object coordinates:", clickPoint);
            trace("stage coordinates:", square.localToGlobal(clickPoint));
            }

            ----------------------------------------------------------
            • 3. Re: Find position of display object
              doug777 Level 1
              As far as I can see, in an automatic layout, these values are not set by the Layout Manager, so getBounds() always returns the default values, not their actual positions in the layout.

              Is this correct?

              Doug