AS3加载图像URLrequest - getBounds不返回任何值

时间:2013-05-19 09:36:29

标签: actionscript-3 loader

我需要获取我正在加载到另一部电影中的旗帜的宽度和高度,以便将其放置在正确的位置。为什么我的getBounds没有拿起旗帜的尺寸?

function displayFlags(evt:Event = null)
{
    if(!Lang) { return; }
    for (var i:uint = 0; i < Lang.length; i++)
   {
       //Language = new MovieClip();
       //Language.name = Lang[i];
       LangButton = new button01();
       LangButton.name = Lang[i];
       LangButton.btext.text = Lang[i];
       LangButton.y = LangButton.height * i;
       addChild(LangButton);

       var flag:Loader = new Loader();
       flag.load(new URLRequest(LangPath[i]+"/flag.png"));

       /// Loads Flag into Button          
       LangButton.addChild(flag);

       var fh = flag.getBounds(flag);
       trace("FLAG HEIGHT = " + fh.height); // ZERO ZERO ZERO ZERO

       // I really need this info to place the flag in the right location. 
       flag.y = (LangButton.height/2) - (flag.height/2);     
   }
evt.target.visible = false;
}

更新日期:2013年5月19日

我能够弄明白我需要等待加载标志。现在我可以得到正确的Bounds ..但是,现在我无法在加载完成时得到movieClip“flag”来响应。我不认为它看到了旗帜的价值。

以下是我的更新代码:

function displayFlags(evt:Event = null)
{
    if(!Lang) { return; }
    for (var i:uint = 0; i < Lang.length; i++)
   {
       //Language = new MovieClip();
       //Language.name = Lang[i];
       LangButton = new button01();
       LangButton.name = Lang[i];
       LangButton.btext.text = Lang[i];
       LangButton.y = LangButton.height * i;
       addChild(LangButton);
       flag = new Loader();
       flag.load(new URLRequest(LangPath[i]+"/flag.png"));
       flag.name = Lang[i];
       flag.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedFlag(flag));
       function loadedFlag()
       {
           return function()
           {
            var fh = flag.getBounds(flag);

           trace("FLAG HEIGHT = " + fh);
           trace("flag Name: "+ flag.name);

           flag.alpha = .3; 
           }
       } 
       LangButton.addChild(flag);
   }
evt.target.visible = false;
}

1 个答案:

答案 0 :(得分:1)

试试这个:

flag.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);

然后添加功能:

function completeHandler(e:Event):void
{
    var myFlagInfo:LoaderInfo = e.currentTarget as LoaderInfo;
    var myFlag:Loader = myFlagInfo.loader;
    var fh = myFlag.getBounds(myFlag);
}