如何将变量传递给Event.COMPLETE函数?

时间:2011-02-06 17:50:23

标签: actionscript-3 flash-cs5

我正在运行一个循环,将拇指从xml列表中拉入包含的movieclip。我想要做的是让拇指的父级动画片段在加载完成后淡入淡出,但我无法弄清楚如何在它加载后引用它。

我的代码(目前无法按照我想要的方式运行):

var vsThumb:articleBox;
var currentarticleX:Number = 0;
var articleLinkURL:String;
var articleImageURL:String;
var articleText:String;
var vsThumbLoader:Loader;
var next_x:Number;
next_x = 9;
var thumbAlphaTween:Tween;
var articlevsThumb:Array = new Array();

function loadarticleHeadlines():void
{
    for (var i:int = 0; i < egarticleXml.articlelist.articleitem.length(); i++)
    {
        vsThumb = new articleBox();
        vsThumb.alpha = 0;
        vsThumbLoader = new Loader();
        vsThumbLoader.load(new URLRequest(egarticleXml.articlelist.articleitem[i].articlethumbnail));
        articleListContainter.addChild(vsThumb);
        vsThumb.articleImage.addChild(vsThumbLoader);
        vsThumb.articleTitle.text = egarticleXml.articlelist.articleitem[i].articletitle;
        titleAutosize(vsThumb.articleTitle);
        vsThumb.x = next_x;
        next_x += 260;
        articlevsThumb[i] = vsThumb;
        vsThumbLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, showBox);
        vsThumb.clickBtn.buttonMode = true;
    }
    function showBox(event:Event):void 
    {
        thumbAlphaTween = new Tween(articlevsThumb[i],"alpha",None.easeNone,0,1,.25,true);
    }
}

那么我如何回访加载器的父级,以便我可以淡入整个动画片段?我可以将变量传递给showBox函数吗?

2 个答案:

答案 0 :(得分:2)

  1. 不要使用嵌套函数。他们往往会使事情变得更复杂。
  2. 我将在您创建的所有事件处理程序中始终具有结束值(articleitem.length()-1),因为其作用域是外部函数loadarticleHeadlines(每次迭代时它将增加1)。这可能就是你的代码无效的原因。
  3. 该事件将在您的加载器的loaderInfo上触发,因此您可以使用event.target.loader.parent找到加载器的父级:

    function loadarticleHeadlines() : void
    {
        for (var i:int = 0; i < egarticleXml.articlelist.articleitem.length(); i++)
        {
            vsThumb = new articleBox();
            vsThumb.alpha = 0;
            vsThumbLoader = new Loader();
            vsThumbLoader.load(new URLRequest(egarticleXml.articlelist.articleitem[i].articlethumbnail));
            articleListContainter.addChild(vsThumb);
            vsThumb.articleImage.addChild(vsThumbLoader);
            vsThumb.articleTitle.text = egarticleXml.articlelist.articleitem[i].articletitle;
            titleAutosize(vsThumb.articleTitle);
            vsThumb.x = next_x;
            next_x += 260;
            articlevsThumb[i] = vsThumb;
            vsThumbLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, showBox);
            vsThumb.clickBtn.buttonMode = true;
        }
    }
    
    
    function showBox(event:Event):void 
    {
        thumbAlphaTween = new Tween(event.target.loader.parent,"alpha",None.easeNone,0,1,.25,true);
    }
    

答案 1 :(得分:1)

您无需将变量传递给showBox,使用targetEvent属性来检索Loader

function showBox(event:Event):void 
{
   var li:LoaderInfo=LoaderInfo(event.target);
   // be nice remove your listener when your are done
   li.removeEventListener(Event.COMPLETE, showBox);

   var ldr:Loader=li.loader; // here is your loader
   // do whatever you want with loader 

    thumbAlphaTween = new Tween(articlevsThumb[i],"alpha",None.easeNone,0,1,.25,true);
}