0 Replies Latest reply on Jan 27, 2011 1:22 AM by scottchu.tw

    How to center loaded swf & get screen coordinates of its component?

    scottchu.tw

      I made an swf using Flash CS4, it has a movieclip named 'mc' & and a vertical bar centered horizontally. I then load it using swfloaded & put it inside a canvas & set it's windows & height to 100%.

       

      The codes:

       

      <mx:Canvas id="cv" verticalScrollPolicy="off" horizontalScrollPolicy="off" width="{screen.width}" height="{screen.height}">
        <mx:SWFLoader id="template" source="assets/sample.swf" width="100%" height="100%"  complete="dump(event);"/>

        <mx:HBox id="bb" />
      </mx:Canvas>

       

      <mx:Script>

      .....

      private function dump(event:Event): void {

       

           var panel:MovieClip = MovieClip(template.content);

           var pt:Point = panel.localToGlobal(new Point(panel.mc.x, panel.mc.y));

           bb.x = pt.x; bb.y = pt.y; bb.width=10; bb.height = 10;

           panel.x = (cv.width - panel.conentWidth) / 2;

           Alert.show(

                "scaleratio x,y = " + panel.scaleX + "," + panel.scaleY +"\n"

                + "panel x,y = " + panel.x + "," + panel.y + "\n"

                + "local x,y = " + panel.mc.x + "," + panel.mc.y + "\n"

                + "global x,y = " + pt.x + "," + pt.y + "\n"

           );

      }
      ....

      </mx:Script>

      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

      The dump result is:

       

      scaleratio x,y = 1,1

      panel x,y = 100,800

      local x,y = 775, 800

      gloval x,y = 875, 800

      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

       

      Q1. I draw an bound box according to the content object of this swf & saw this bound box is indeed centered. However, the bar object is some offset left from the center of screen. Why it's not aligned center as I saw it in Flash CS4?

       

      Q2. I try to get the screen coordinates of component 'mc' by using localToGlobal to convert it to screen coordinates & draw a box to overlap their top-left point. However the topleft point of box is far left of mc's on screen. I think it's due to the swf is scaled, so I dump scaleX and scaleY of this swf but they are both 1? How can I get correct screen coordinates of mc?

       

      Thanks in advance!

       

      Scott