Recently I was trying to get the height and width of an image loaded into Flash but kept getting zero for height and width. Turns out I was getting the dimensions before the picture was completely loaded .
Instead, I used the movieClipLoader object to load the picture. Then I could use a listener object to detect the onLoadInit event of the movieClipLoader object so that I could get the dimensions after the picture was completely loaded.
Here's the movieClipLoader related code in which the height and width are captured in the image movieclip's orig_height and orig_width variables.
- // ************************************************************
-
// Create object to listen for when done with loading pic
-
// ************************************************************
-
var loadListener:Object = new Object();
-
loadListener.onLoadInit = function(target_mc:MovieClip):Void {
-
target_mc.orig_height = target_mc._height;
-
target_mc.orig_width = target_mc._width;
-
};
-
var mcLoader:MovieClipLoader = new MovieClipLoader();
-
mcLoader.addListener(loadListener);;
-
-
// ---------------------------------------------------------------------------
-
// Use MovieClipLoader to load the image
-
// ---------------------------------------------------------------------------
-
mcLoader.loadClip("pic.png", image);
todd
there are other methods available for the MovieClip.loadClip() that might be very useful. just grab add them to todd's code and you're sorted.
onLoadError, in case of an error
onLoadProgress, during the load
onLoadStart, when it starts loading
onLoadComplete, when it finishes loading
onLoadInit, when it is loaded and goes to the first keyframe (that's why using any of the previous methods would return zero, cos it was empty, but when it checks the first keyframe it is filled with your image)
for example:
loadListener.onLoadError = function(target_mc:MovieClip):Void {
trace('not loaded: ' + mc);
};
HTH :)
dns