[英]AS3: Cant get the length of dynamically created MC, outside of its function
I want to get the size of parent_mc after it has had all the content of a dynamic folder loaded, I tried : 我想在加载动态文件夹的所有内容之后得到parent_mc的大小,我试过:
function initmyWidth():Number
{
var myWidth:Number = galleryArr.length * 400;
return myWidth;
}
it gives me the correct number if its nested in function onGalleryLoaded but outside of the fun ton it traces 0. there a way to pass galleryArr.length outside the onGalleryLoaded function? 它给了我正确的数字,如果它嵌套在函数onGalleryLoaded中,但在乐趣之外,它跟踪0.有一种方法可以将galleryArr.length传递到onGalleryLoaded函数之外吗?
var galleryArr:Array = [];
var parent_mc:Sprite = new Sprite();
addChild(parent_mc);
parent_mc.y = 112;
parent_mc.x = 300;
function onGalleryLoaded(e:Event):void
{
var imageX:int = 0;
var imageY:int = 0;
for each (var image:Sprite in galleryArr)
{
var my_mc:MovieClip = new MovieClip();
my_mc.addChild(image);
parent_mc.addChild(my_mc);
my_mc.width = 400;
my_mc.height = 400;
my_mc.x -= parent_mc.width;
my_mc.y = imageY;
imageX -= my_mc.width + PADDING_X;
if ( (imageX+my_mc.width) > stage.stageWidth )
{
imageX = parent_mc.width;
imageY = 0;
}
}
}
You cannot expect to get an accurate width until you know how many elements are in gallyerArr
. 在知道gallyerArr
有多少元素之前,您不能指望获得准确的宽度。 This isn't a case of passing a value from outside of a function, but rather a case of attempting to do something synchronously while an asynchronous process hasn't completed (in this case, your load event). 这不是从函数外部传递值的情况,而是在异步进程尚未完成时尝试同步执行某些操作的情况(在本例中为load事件)。
Once onGalleryLoaded
has been called, initmyWidth
will function as expected. 一旦onGalleryLoaded
被调用, initmyWidth
将如预期运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.