0

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 so 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;
        }
    }
}

}

3 Answers 3

2

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();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant! I like how you loop between 2 different functions, using the event listener to re-initate the loading process! This opens up the door for other things I am trying to do..... Your right, too, that the error comes from the "loader.content.x = i * 240;" line. I found that out shortly after I posted. Thanks for all the help!!
1

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;
            }
        }
    }
}

Comments

0

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.

1 Comment

looks very useful. I just figured out my own loader, but will likely look to this one for future programs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.