dejjub-AIS
dejjub-AIS

Reputation: 1541

flex 4 image dimensions

I am trying to get the image dimensions to center in the dynamic sprite object but I am getting 0 in the height and widh property. Can some one please help me to get the image dimensions? I tried by using various properties of image control but I am getting the dimensions. Please see code below and the values in the inline comments

mxml code:

<mx:Image id = "imgActive" visible="false" />

event handler:

  private function addImageHandler(e:MyCustomeEve){
            imgActive.source = e.imageUrl; 
            imgActive.visible = true;

            GAME_STATUS = "ITEM_SELECTED";
            trace(imgActive.width,
                imgActive.height, // 0
                imgActive.contentHeight, // 0
                imgActive.contentWidth, // NaN
                imgActive.content.width, // error because imgActive.content is null
                imgActive.content.height  // error because imgActive.contentis null);
            imgActive.cacheAsBitmap = true;
}

Upvotes: 0

Views: 642

Answers (1)

Anton Petrov
Anton Petrov

Reputation: 704

When you set the source of the image you'll have to wait for it to load the content. Add an event listener like imgActive.addEventListener(Event.COMPLETE, onImgActiveComplete);

When the image is loaded, the onImgActiveComplete handler function will be called and you can then get the dimensions of the image for example by using imgActive.content.width and imgAcive.content.height respectively.

Hope this helps,

Blaze

Upvotes: 2

Related Questions