如何在Flash中设置加载图像的宽度和高度?请求后立即设置尺寸不起作用。宽度和高度保持为零。在Flash中设置加载图像的尺寸
var poster:Loader = new Loader();
Stage.addChild(poster);
poster.load(new URLRequest('http://example.com/image.jpg'));
poster.width = 320;
poster.height = 240;
trace(poster.width); // 0
trace(poster.height); // 0
如果我稍等片刻,然后设置尺寸,它将工作。
我尝试在调整大小之前听一些教程中提到的Event.INIT
事件和Event.COMPLETE
事件。这两件事都没有被触发。
public function theClass() {
this.poster = new Loader();
this.poster.contentLoaderInfo.addEventListener(Event.INIT, this.imageLoaded);
this.poster.contentLoaderInfo.addEventListener(Event.COMPLETE, this.imageLoaded);
Stage.addChild(this.poster);
this.poster.load(new URLRequest('http://example.com/image.jpg'));
}
private function imageLoaded(event:Event):void {
trace('image is loaded');
this.poster.width = 320;
this.poster.height = 240;
}
也许您应该在您的答案中添加设置尺寸的部分? – Taurayi 2011-04-11 23:25:26
我不敢相信我没有想到要那样做。它只是在onComplete函数中设置e.target.content,我现在就这样做。 – jhocking 2011-04-12 01:40:44
我知道,似乎我一直在挑剔你的答案,但如果我不得不提出最后一件事情,那么可能是在事件处理程序内部将'e.target'对象存储在局部变量中。就像'var loader:Loader = Loader(e.target);'然后将'loader.content'对象的位图存储在一个局部变量中,比如'var bitmap:Bitmap = Bitmap(loader.content);',之后你可以简单地做'bitmap.width = 100; bitmap.height = 100;'。不要误会我的意思你的答案已经正确了,只是将事件目标对象存储在本地变量中是一个常见的约定。 – Taurayi 2011-04-12 11:28:23