开发者

loading external swfs with a loop, AS3

I've spent days working on this code, it seems so simple, but yet it never works. All I'm trying to do is make a loop automatically load person0.swf through person4.swf in the same directory. I've got it set-up s开发者_开发问答o that I can change a single number and it loads up to person[whatever-the-new-number-is].swf

Currently it loads the first movie and thats it. I also get this error:

"TypeError: Error #1009: Cannot access a property or method of a null object reference. at Menu()"

Here is the code

package {

import flash.display.*;
import flash.net.URLRequest;
import flash.events.*;

public class Menu extends MovieClip {
    var loaders:Array;
    var loadedCount:uint;
    var person:Array = new Array();
    var filenum:int = 5;

    function Menu() {
        for (var i = 0; i < 4; i++) {
            var loader = new Loader();
            person.push("person" + i + ".swf"); 
            var currentperson = person[i]; 
            var url:URLRequest = new URLRequest(currentperson);
            loader.load(url);
            addChild(loader);
            loader.content.x = i * 240;
        }
    }
}

}


I think what is happening is that loader doesn't have time to complete the load process before you try to move the position of the loader content. Since the content isn't yet in place it returns an error because the content is null.

If you comment out the "loader.content.x = i * 240;" line you'll see that it runs fine .

So what you might want to do is move the content after the loader dispatches a "complete" event by adding an event listener to the loader's contentLoaderInfo property.

Try this:

private var person:Array = new Array();     
private var itemCount:Number = 0;
private var itemMax:Number = 4;

public function Main():void 
{
    loadSWFs();
}

private function loadSWFs()
{
    person.push("person" + itemCount + ".swf");
    var currentPerson = person[itemCount];
    var url:URLRequest = new URLRequest(currentPerson);
    var loader:Loader = new Loader();
    loader.load(url);
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, moveLoaderChild);
    addChild(loader);
}

private function moveLoaderChild(e)
{
    e.target.content.x = itemCount * 240;
    if (itemCount < itemMax)
    {
        itemCount++;
        loadSWFs();
    }
}


I also figured out another solution, although BigLarry's is a bit more sophisticated. The error disappeared when I got rid of the .content from the previous line loader.content.x.

Also, by storing each loader and URLRequest inside an array, all of the swfs showed up on screen instead of the same one being replaced repeatedly.

package {
    import flash.display.*;
    import flash.net.URLRequest;
    import flash.events.*;

    public class Menu extends MovieClip {
        public function Menu() {
            var loaders:Array = new Array();
            var person:Array = new Array();
            var url:Array = new Array();
            var filenum:int = 5;

            for (var i = 0; i < filenum; i++) {
                loaders.push(new Loader());
                person.push("person" + i + ".swf");  
                url.push(new URLRequest(person[i]));
                loaders[i].load(url[i]);
                addChild(loaders[i]);
                loaders[i].x = i * 240;
            }
        }
    }
}


You should use LoaderMax by Greensock (the creator of TweenLite/TweenMax).

http://www.greensock.com/loadermax/

It's very handy, you just pass in an array of file paths and it does the rest. Checkout the live demo at the above link, and you'll be convinced.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜