麻烦分离装载程序数组和数组网格的图像

时间:2009-12-15 00:06:35

标签: actionscript-3

我的问题是我无法弄清楚如何将加载器与此loadNewRow函数分离。理想情况下,我想弄清楚如何加载所有swf给它们动态名称...像thmb1loaded,thmb2loaded,thmb3loaded ..推入一个(“加载”)数组然后用它来构建网格

现在,每次loadnewrow函数触发时都会加载它们。我确定在每个swf加载的时候在contentloaderinfo .onComplete中迭代一个.name是一件简单的事情...任何帮助都会感激,这让我疯狂了几天..

var filePaths:Array=["thmb1.swf","thmb2.swf","thmb3.swf","thmb4.swf", "thmb5.swf", "thmb6.swf",
 "thmb7.swf","thmb8.swf", "thmb9.swf", "thmb10.swf", "thmb11.swf" ];

function loadImages3(event:MouseEvent):void {
    TweenLite.to(pic, 8, {alpha:0, ease:Expo.easeInOut});// dim image container
    trace("loadImages3 fired ---------------------------------------------------------")
    unloadImages3resize(); loadImages3Flag=true;
    var randomScale:Number = .3 + Math.random();trace("in loadimages3 function randomScale is now.. " + randomScale);
    var stage_w=stage.stageWidth;trace("in loadimages3 function stage_w is = " + stage_w);
    var stage_h=stage.stageHeight;trace("in loadimages3 function stage_h is = " + stage_h);
    var thumbWidth:Number=240;var thumbHeight:Number=155;//var randomScale:Number=.45;
    var scaleThumbs:Number=randomScale;
    var finalRandomScaledWidth:Number=thumbWidth*scaleThumbs;
    var finalRandomScaledHeight:Number=thumbHeight*scaleThumbs;
    var clipsFitX=Math.floor(stage_w/finalRandomScaledWidth);
    trace("in loadimages3 function finalRandomScaledWidth is = " + finalRandomScaledWidth);
    var clipsFitY=Math.floor(stage_h/finalRandomScaledHeight);
    trace("in loadimages3 function finalRandomScaledHeight is = "  + finalRandomScaledHeight);
    var clipsFitXTotal:Number = clipsFitX+1; // add extra one on the horizontal
    var clipsFitYTotal:Number = clipsFitY+2; // add extra two on the vertical
    trace ("ClipsFitXtotal) = " +clipsFitXTotal, "(clipsFitYTotal) = " + clipsFitYTotal);
    loadnewRow(clipsFitXTotal, clipsFitYTotal,randomScale,finalRandomScaledHeight, finalRandomScaledWidth );
}

function loadnewRow(clipsFitXTotal:Number, clipsFitYTotal:Number, randomScale:Number, 
                    finalRandomScaledHeight:Number, finalRandomScaledWidth:Number):void {
    trace("loadnewRow fired ---------------------------------------------------------");
    trace("randomScale out of loop but in loadnewRow function loop =" + randomScale);
    for (var z:Number =0; z < clipsFitYTotal; z++) {
        for (var b:Number = 0; b < clipsFitXTotal; b++) {
            var ldr:Loader = new Loader();
            var randomSelection = Math.floor( (Math.random()*filePaths.length) );
            ldr.load(new URLRequest (filePaths[randomSelection]));
            ldr.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
            ldr.scaleX=ldr.scaleY=randomScale;//trace("randomScale in the loop is =" + randomScale);
            ldr.y=finalRandomScaledHeight*z;//trace("finalRandomScaledHeight*z =" + ldr.y);
            ldr.x=finalRandomScaledWidth*b;//trace("finalRandomScaledWidth*b =" + ldr.x);
            ldr.alpha=.8;
            //trace("[ldr] = number of children = " + ldr.numChildren);
            //trace("[THIS] = number of children = " + this.numChildren);
            //trace("[THIS] name is = " +this.name);
            ldr.cacheAsBitmap = true;
            //thumbholder.addChild(ldr);
            addChildAt(ldr,1);
            filePaths.splice(randomSelection, 1);
            if (filePaths.length==0) {
                filePaths.push("thmb1.swf","thmb2.swf","thmb3.swf","thmb4.swf","thmb5.swf",
                    "thmb6.swf","thmb7.swf","thmb8.swf", "thmb9.swf",
                "thmb10.swf", "thmb11.swf");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我建议你使用一些库/框架来简化事情。当我使用splinklibrary时,这里有一些示例代码可以借助splinklibrary解决您的问题:

    public function Test() {
        var filePaths : Array = ["thmb1.swf","thmb2.swf","thmb3.swf"];

        var q : IQ = new Q();
        q.register(QCompleteEvent.COMPLETE, onComplete);
        for each (var path : String in filePaths) {
            q.add(new QLoader(new URLRequest(path)));
        }
        q.start();
    }

    private function onComplete(e : QCompleteEvent) : void {
        var displays : Array = new Array();
        var q : IQ = IQ(e.getSource());
        for each (var loader : QLoader in q.getItems()) {
            displays.push(addChild(loader.getContent()));
        }

        arrange(displays);
        show(displays);
    }

    private function arrange(displays : Array) : void {
        // put in your code to arrange the loaded displayobjects
    }

    private function show(displays : Array) : void {
        // put in your code to tween the loaded displayobjects
    }

当然,您也可以在没有图书馆的情况下实现目标,但重点是图书馆通常有助于保持您的代码简洁明了。