AS3加载完成后显示图像

时间:2011-04-03 15:16:46

标签: actionscript-3 action

所以我正在从xml加载图片,并将它们添加到一个名为cv的movieclip中,它有一个名为cHolder的持有者。现在的问题是,当预加载器显示它正在加载时,cv(s)已经出现了。无论如何只有在图像完成加载后显示所有的cv? 感谢。

for each (var projectName:XML in projectAttributes) 
    {
        //trace(projectName);
        var projectDP:XMLList = projectInput.project.(@name == projectName).displayP;
        //trace(projectDP);

        var cv:MovieClip = new cView();
        catNo += 1;
        cv.name = "cv" + catNo;
        cv.buttonMode = true;
        if(catNo % 5 == 0)
        {
            catY += 137;
            catX = -170;
            cv.x = catX;
            cv.y = catY;
        }
        else
        {
            cv.x = catX;
            cv.y = catY;
            catX += 112;
        }

        var imageLoader = new Loader();
        imageLoader.load(new URLRequest(projectDP));        
        TweenLite.to(cv.cHolder, 1, {colorTransform:{tint:0x000000, tintAmount:0.8}});
        cv.cHolder.addChild(imageLoader);
        cv.ct.text = projectName;
        projName.push(projectName);
        this.addChild(cv);

        imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageProg);
        function imageProg(e:ProgressEvent):void
        {
            loader.visible = true;
            var imageLoaded:Number = e.bytesLoaded/e.bytesTotal*100;
            loader.scaleX = imageLoaded/100;
        }

        imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoad);
        function imageLoad(e:Event):void
        {
            loader.visible = false;
        }       

2 个答案:

答案 0 :(得分:1)

首先,不要在另一个函数中放置一个函数,这对任何事都没有帮助,这是一个坏习惯:)

声明两个私有变量:

var nImages:uint;
var loadedImages:uint;

循环之前:

nImages = projectAttributes.length();
loadedImages = 0;
cv.visible = false;

Event.COMPLETE听众:

function imageLoad(e:Event):void
{
    loader.visible = false;
    loadedImages++;
    if (loadedImages == nImages)
    {
        cv.visible = true;
    }
}

答案 1 :(得分:0)

  

var count:int = 0;

     

var totalClips:int = 0;

     

cv.visible = false;

for each (var projectName:XML in projectAttributes) 

{
    ++totalClips;
    //trace(projectName);
    var projectDP:XMLList = projectInput.project.(@name == projectName).displayP;
    //trace(projectDP);

    var cv:MovieClip = new cView();
    catNo += 1;
    cv.name = "cv" + catNo;
    cv.buttonMode = true;
    if(catNo % 5 == 0)
    {
        catY += 137;
        catX = -170;
        cv.x = catX;
        cv.y = catY;
    }
    else
    {
        cv.x = catX;
        cv.y = catY;
        catX += 112;
    }

    var imageLoader = new Loader();
    imageLoader.load(new URLRequest(projectDP));        
    TweenLite.to(cv.cHolder, 1, {colorTransform:{tint:0x000000, tintAmount:0.8}});
    cv.cHolder.addChild(imageLoader);
    cv.ct.text = projectName;
    projName.push(projectName);
    this.addChild(cv);

    imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageProg);
    function imageProg(e:ProgressEvent):void
    {
        loader.visible = true;
        var imageLoaded:Number = e.bytesLoaded/e.bytesTotal*100;
        loader.scaleX = imageLoaded/100;
    }

    imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoad);
    function imageLoad(e:Event):void
    {
        ++count;
        if(count == totalClips){
             cv.visible = true;
        }
        loader.visible = false;
    }

}

所以你可能想稍微适应一些事情,以防我为你的实现计算错误的位置等但是你可以看到解决方案很简单,你可以计算正在处理的剪辑总数。 XML,然后在加载图像并调用OnComplete回调时,增加另一个计数,直到您看到已加载所有已处理的图像,并将容器设置为可见。