4 Replies Latest reply on May 6, 2011 10:38 AM by imfromNeverland

    how to get an Image.height which created at runtime?

    imfromNeverland

      hi

       

      i am creating an image at runtime and adding it to a group control. after that i want to get this images width and height.

      but when i get image like this:

       

      var bgImg:Image = group_createdMenu.getElementAt(imageIndex) as Image;

       

      bgImg.height = 0; --> not coming the sources height.

       

      i think because of the image not drawned yet. What can i do?

       

      Thank you so much, have a good day.

        • 1. Re: how to get an Image.height which created at runtime?
          Lee Burrows Level 4

          hi

           

          without seeing your code its difficult to assess what the problem may be but these options may help troubleshoot/fix it:

           

          check that group_createdMenu.getElementAt(imageIndex) is returning an image - add trace(bgImg); after var bgImg:Image = ... - if it outputs "null" then then its not an image type (so you cant get its height)

           

          try getting the dimensions from the bitmapData object instead: bgImg.bitmapData.height

           

          or post the code where you create the image - are you loading a bitmap at runtime or generating it with code?

          • 2. Re: how to get an Image.height which created at runtime?
            imfromNeverland Level 1

            hi again,

             

            i am coding a package which can create a dynamic menu at runtime with parameters.

            so i am adding buttons' background images in a folder in project and giving them sources in an array in code.

            creating a group and then creating an image and adding it to created group with .addElement.

            after added first image for first button, i need to change second image's x and y to add to group. (because to add them with an order consecutively)

            so i need to calculate exact width and height for x and y.

            as you said, i tried something like this..

             

            var u:URLRequest = new URLRequest(imgUrl);

             

             

            l.load(u);

            l.contentLoaderInfo.addEventListener(Event.COMPLETE, done);

             

            public function done(e:Event):void

            {

                 var img:Bitmap =  Bitmap(l.content);

                 var h:int = img.height;

            }

             

            now i can get the height and so width too but i am not so sure is it the correct way to handle it or can i do it in a better way?

            • 3. Re: how to get an Image.height which created at runtime?
              Lee Burrows Level 4

              yep - thats the best way to do it.

               

              for completeness, i suggest adding l.contentLoaderInfo.removeEventListener(Event.COMPLETE, done); to the done method - its always best practise to remove an event listener once you no longer need it

              • 4. Re: how to get an Image.height which created at runtime?
                imfromNeverland Level 1

                well eureka!

                 

                i'm glad to hear it. thank you so much. i wish you have a perfect weekend.