PDA

View Full Version : How to preload a SWF?


LuisPedro
10-21-2007, 08:40 AM
I'm trying to make a preload of the main.swf from the preload.swf.

The preload.swf code is:
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.text.TextField;
import flash.display.MovieClip;

var myLoader:Loader = new Loader();
var myRequest:URLRequest = new URLRequest("main.swf");

myLoader.load(myRequest);

myLoader.contentLoaderInfo.addEventListener(Event. OPEN,showPreloader);
myLoader.contentLoaderInfo.addEventListener(Progre ssEvent.PROGRESS,showProgress);
myLoader.contentLoaderInfo.addEventListener(Event. COMPLETE,showLoadResult);

var loadProgress_txt:TextField = new TextField();

function showPreloader(evt:Event):void {
addChild(loadProgress_txt);
}

function showProgress(evt:ProgressEvent):void {
loadProgress_txt.text = "loaded:"+evt.bytesLoaded+" from "+evt.bytesTotal;
}

function showLoadResult(evt:Event):void {
removeChild(loadProgress_txt);
addChild(myLoader);
}

And it gives me this errror:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at main_fla::MainTimeline/main_fla::frame1()

The problem is that i don't know how to do this preload and copy that code from another page...

Can anyone helpe with this, or explain how to build a preload with a progress bar tha realy work?

Tnks in advance

LP

nikefido
10-21-2007, 03:24 PM
Does your text field get added to the stage?

Try not making adding the text field dependent on the eventListener.


Also - what line of code is the error on (if any is given) - this looks like the type of error you get if you start refering to "stage" or "root" before items are added to the main timeline.

import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.text.TextField;
import flash.display.MovieClip;

var myLoader:Loader = new Loader();
var myRequest:URLRequest = new URLRequest("main.swf");

myLoader.load(myRequest);

//myLoader.contentLoaderInfo.addEventListener(Event. OPEN,showPreloader);
myLoader.contentLoaderInfo.addEventListener(Progre ssEvent.PROGRESS,showProgress);
myLoader.contentLoaderInfo.addEventListener(Event. COMPLETE,showLoadResult);

var loadProgress_txt:TextField = new TextField();

//function showPreloader(evt:Event):void {
addChild(loadProgress_txt);
//}

function showProgress(evt:ProgressEvent):void {
loadProgress_txt.text = "loaded:"+evt.bytesLoaded+" from "+evt.bytesTotal;
}

function showLoadResult(evt:Event):void {
removeChild(loadProgress_txt);
addChild(myLoader);
}

LuisPedro
10-21-2007, 04:12 PM
Yes. The text field is added on the stage.
The erros is not in the ActionScript timeline but in the Erros on the Preview.

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

Nikefido,, send you a PM.

LuisPedro
10-21-2007, 06:14 PM
I try the preload.swf code with a JPG file and it work fine.

Only when i change URLRequest() to any SWF file it brings me that error, so i think that is relater with the method I call it (addChild) or with the code in main.swf!!

I think that is the method i call it because maybe that's not possible to addChild with a SWF file...

Any sugestions?

stompwampa
10-21-2007, 06:58 PM
no, addChild is exactly what you want...that's what I used in my preloaders...your code looks good though, so I'm not sure what the issue is...

nikefido
10-21-2007, 10:04 PM
did you uncheck the property to load the AS etc on frame 1?
There's an option for it, but I cannot currently remember where it is :(

LuisPedro
10-21-2007, 10:45 PM
did you uncheck the property to load the AS etc on frame 1?
There's an option for it, but I cannot currently remember where it is :(

I have to uncheck it all????
But if i do that the AS doesent recognize... :confused:

But i do that and when i test it, it became a lot of erros.

I don't think that is the solution.

Remember i don't have anything on stage, it's all became from the AS timeline.

nikefido
10-22-2007, 01:20 AM
I have to uncheck it all????
But if i do that the AS doesent recognize... :confused:

But i do that and when i test it, it became a lot of erros.

I don't think that is the solution.

Remember i don't have anything on stage, it's all became from the AS timeline.

nope, i wanted you to make sure you did NOT uncheck it, sorry for the confusion.