package
{
import flash.display.*;
import flash.events.*;
import flash.net.URLRequest;
public class Main extends MovieClip
{
// state
private var iname:String = "image.gif";
private var w:int;
private var h:int;
private var loader:Loader;
// constructor
public function Main():void
{
loadImage( iname );
trace( w + " " + h );
}
// methods
private function loadImage( filename:String ):void
{
loader = new Loader();
loader.contentLoaderInfo.addEventListener( Event.COMPLETE, completeHandler );
loader.contentLoaderInfo.addEventListener( Event.COMPLETE, someHandler );
var request:URLRequest = new URLRequest( filename );
loader.load( request );
this.addChild( loader );
}
// event handlers
private function completeHandler( event:Event ):void
{
trace( "image loaded\n" + event.target.url );
trace( event.target );
}
private function someHandler( event:Event ):void
{
w = loader.contentLoaderInfo.width;
h = loader.contentLoaderInfo.height;
}
}
}
但是当我跑步时我的宽度和高度都是0 0。我怎样才能得到正确的值? 此外,如果图片正确加载并在我将它附加到movieclip后为什么我不能从movieclip对象获取宽度和高度?
答案 0 :(得分:1)
仅仅是我还是在完成事件发生之前要求宽度和高度?如果在设置它们之后将w和h跟踪移动到“someHandler”函数中,会得到什么结果?
答案 1 :(得分:0)
图像的加载不是即时的 跟踪一个Event.COMPLETE处理程序的宽度和高度,它会正常工作!
答案 2 :(得分:0)
你要求的是contentLoaderInfo.height ...你应该要求内容高度。
h = event.target.content.height;
w = event.target.content.width;