0
votes

I am a Flash and ActionScript newbie. I am trying to follow a video tutorial to make a preloader and I'm having a problem that the video didn't seem to address. I believe I have entered in all of the code correctly from the video. This is it:

stop();

addEventListener(Event.ENTER_FRAME, loaderF);

function loaderF(e:Event):void{
    var toLoad:Number = loaderInfo.bytesTotal;
    var loaded:Number = loaderInfo.bytesLoaded;
    var total:Number = loaded/toLoad;
    if( loaded == toLoad ){
        removeEventListener(Event.ENTER_FRAME, loaderF);
        gotoAndStop(2);
    } else {
        preloader_mc.preloaderFill_mc.scaleX = total;
        preloader_mc.percent_txt.text = Math.floor( total * 100 ) + "%";
        preloader_mc.ofBytes_txt.text = loaded + "bytes";
        preloader_mc.totalBytes_txt.text = toLoad + "bytes";
    }
}

What I typed in doesn't generate a compiler error, but the output tells me:

TypeError: Error #1010: A term is undefined and has no properties.
at preloader_fla::MainTimeline/loaderF()

And since I really don't have any experience outside of what I'm learning from this tutorial series, I don't know what to do to fix this.

2

2 Answers

0
votes

I don't use Flash CS5, but you should be able to get the line # for where the error is occurring, I believe, by executing the SWF by pressing CTRL+SHIFT+ENTER.

Once you get the line number, you should see that something on that line is null or not defined. The error says it occurs in the function loaderF(), and looking at that code the only place such an error could occur is in the else block:

} else {
        preloader_mc.preloaderFill_mc.scaleX = total;
        preloader_mc.percent_txt.text = Math.floor( total * 100 ) + "%";
        preloader_mc.ofBytes_txt.text = loaded + "bytes";
        preloader_mc.totalBytes_txt.text = toLoad + "bytes";
    }

In the above code block, one of these things is not defined: preloader_mc.preloaderFill_mc, preloader_mc.percent_txt, preloader_mc.ofBytes_txt, preloader_mc.totalBytes_txt

Maybe your preloader movie clip is missing one of these objects...

0
votes

First, you'll want to turn on debugging found under (File > Publish Settings > Flash (.swf) > Permit Debugging). This will provide line numbers and allow additional debugging to help track down errors.

Secondly, in the code sample you've provided, you haven't declared a loader, so when you call on loaderInfo, it makes sense that flash complains about "a term is undefined". Although, technically, the loaderInfo object is a child of the event object. Thus, loaderInfo.bytesTotal would become e.loaderInfo.bytesTotal, assuming you added the event listener to the loader object; currently yours is added to the timeline.

Bookmark Adobe's Actionscript 3.0 Reference. Use it. As you begin your journey in Flash, this will be your indispensable handbook to speaking AS3. Specifically, you'll want to refer to the Loader class.

Here's what you're likely missing in your code:

var myLoader:Loader = new Loader();
myLoader.load(new URLRequest("path/to/my/file"));

Your function loaderF is being called during every frame update to the screen (likely every .034 seconds). You'd probably be happier with ProgressEvent.PROGRESS instead of Event.ENTER_FRAME. If so, you'll also want to catch the complete event, and that'd look like this:

myLoader.addEventListener(Event.COMPLETE, loadComplete);
myLoader.addEventListener(ProgressEvent.PROGRESS, loadProgress);

function loadComplete(e:Event):void {
    // Stuff to do when the file finishes loading.
}

function loadProgress(e:Event):void {
    var current:int = e.bytesLoaded;
    var total:int = e.bytesTotal;
    var percent:Number = current/total;

    // Update the readout of your loading progress.
}

Hopefully that points you in the right direction. :)