在 AS3 中查找(加载)图像大小(动作脚本 3.0)
我目前使用以下函数来加载图像,但是我无法找到一种方法来找到加载图像的宽度,我打算在使用相同函数放置下一个图像之前使用它。
请注意,q 是一个变量(数字),用于加载不同的图像。
=X 我需要帮助获取加载的图像宽度...
function LoadImage(q)
{
var imageLoader:Loader = new Loader();
var image:URLRequest = new URLRequest("GalleryImages/Album1/"+q+".jpg");
imageLoader.load(image);
addChild (imageLoader);
imageLoader.x = 0 + CurrentXLength;
imageLoader.y = 0;
imageLoader.name = "image"+q;
trace(imageLoader.x)
}
Im currently using the following function to load an image, however i could not figure out a way to find the width of the loaded image, which i intend to use before placing the next image using the same function.
Note that q is a a variable (a number) which is used to load differant images.
=X i need help obtainning the loaded image width...
function LoadImage(q)
{
var imageLoader:Loader = new Loader();
var image:URLRequest = new URLRequest("GalleryImages/Album1/"+q+".jpg");
imageLoader.load(image);
addChild (imageLoader);
imageLoader.x = 0 + CurrentXLength;
imageLoader.y = 0;
imageLoader.name = "image"+q;
trace(imageLoader.x)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在实际加载之前,您无法知道位图的宽度:
或者 此链接可能有帮助吗? 自己没试过...
You can't know the width of the bitmap until it's actually loaded:
Alternativly this link might be helpful? Haven't tried it myself ...
我只是询问加载器对象的 de width 属性:
其中 1360 和 768 是画布尺寸......
I just asked for de width property of loader object:
Where 1360 and 768 are the canvas dimensions...
要访问宽度,您必须在分配给处理 Event.COMPLETE 的函数中执行此操作。
“您将需要一个包含您希望加载的项目的数组。您可能应该使用 XML 加载此数据,以便它是动态且可扩展的。加载 xml 数据后,应该以您喜欢的任何方式将其分配给数组。在这种情况下,我们必须使用数组,而不仅仅是使用 XML 对象(它本质上是一个数组),是因为您需要知道对象宽度,以便可以将下一个对象 X 位置置于最后一个对象 X 的基础上 。
对于 XML,通常使用 for 循环并迭代“x”次,在这种情况下,要获取加载资源的“WIDTH”属性 当加载程序触发 Event.COMPLETE 时,可以从指定为触发的函数内进行访问。 一旦图像完成,它将从数组中删除该项目,设置一个变量作为 lastX 和 lastWidth,然后获取数组中的下一个项目。最终数组为空,整个过程完成。
-注意:我将跳过加载 XML,而直接将数据注入到数组中。
我希望这有帮助,代码未经测试,但这个概念似乎是有效的。
To access the width you must do it within the function assigned to handle Event.COMPLETE.
"You will want an array containing the items you wish to load. You should probably load this data in with XML so it is dynamic and scalable. Once the xml data is loaded it should be assigned to an array in whatever fashion you like. The reason that we must use an array in this situation, rather then just using the XML object, which is essentially an array, is because you need the know an objects width so that you can base the next objects X position off of the last objects X position plus its WIDTH.
With XML it is common to use a for loop and just iterate through "x" amount of times. We do not want this, in this case. To obtain the "WIDTH" property of the loaded asset, it must be accessed from within the function assigned to fire when the loader fires Event.COMPLETE. Once the image has completed it will remove the item from the array, set a variable as to the lastX and lastWidth, and then get the next item in the array and start all over. Eventually the array is empty and the process is complete.
-Note: I will skip loading the XML and just inject the data into the array myself.
I hope this helps, the code isn't tested, but the concept seems valid.
是的,我使用了 scott 的答案,但值得注意的是 LoadImage() 函数中的“imageLoader.contentLoader”应该是“imageLoader.contentLoaderInfo”。 为我解决了宽度问题——谢谢 mang
Yeah I used scott's answer but it's worth noting that 'imageLoader.contentLoader' should be 'imageLoader.contentLoaderInfo' in the LoadImage() function. Solved the width prob for me-- thanks mang