PDA

View Full Version : preloading and exporting classes


Flash Gordon
10-08-2007, 05:32 PM
How do you exports classes to a frame other than 1 in the publish settings and uncheck the linkage box for assets not to export on frame 1 and still get AS 3.0 to work?

Attached is a simple example. I've tried numerous ways to set it up to mimic a real swf set up for preloading, but I can't get a blue box to display.

Please help!!!!!!

charlieFlash
10-23-2007, 01:38 PM
Found a tute link that may help:
http://www.bit-101.com/blog/?p=946

It's not specifically accomplishing what you are asking, but it does deal with the issue of how to preload assets exporting before frame 1 in AS3...

Flash Gordon
10-23-2007, 06:01 PM
Yea, I saw that too, but it is Flex. I'm not sure how to translate that to Flash.

FrodoBaggins
10-31-2007, 03:27 PM
nobody wants to comment on this, eh?
I would if I had something useful to say about it.

Flash Gordon
11-01-2007, 09:50 PM
Actually that does help out. It lets me know it isn't just a dumb question but other people don't know the answer as well. My quote about got delete by a mod I didn't delete it, but along with that I wrote I don't think it is possible. I think AS 3.0 preloads differently than AS 2.0. I'm hoping someone will eventually post some help or a definitive answer as currently I'm just guessing. And as you can see, I've been looking for this answer for a month now.

Flash Gordon
11-21-2007, 07:54 PM
$100 USD bucks via PayPal if someone wants to make an AS 3.0 preloader for me (basically this problem). I just need a definate an answer to this issue. If you are doing it for the money, PM me first so I don't have 10 people doing this.

panel
11-21-2007, 10:02 PM
Flash Gordon you must be very desperate :]

Flash Gordon
11-24-2007, 09:00 PM
Yes, I'm getting there. I feel it is a worthy question and it appears not many know how to answer it. OR it is such a bad question, nobody will answer it.

All of my AS 3.0 websites have around 32K bytes on the first frame. I'd like to see that number drop to about 4K like my AS 2.0 sites.

Flash Gordon
11-27-2007, 10:50 PM
$150 USD via PayPal

How much do I have to offer to pay my fellow members of AS[org] to get this answered? Or how many times must I bump this thread?

BrettAF3D
11-27-2007, 10:58 PM
I feel your pain my flash friend, but unfortunately i can't help you on this one... good luck ;)

Flash Gordon
11-27-2007, 11:00 PM
thanks for posting. I appreicate knowing that other people are lost on this as well.

It seems like with so many AS 3.0 sites, someone would know this simple answer.

Thanks again and have a good day :)

BrettAF3D
11-27-2007, 11:27 PM
it reminded me of something i stumble upon going through the help file, the RuntimeAssetExplorer sample in ActionScript3.0_Flash_Samples.zip (ZIP, 8.10MB)
http://www.adobe.com/support/documentation/en/flash/samples/

i don't know if it would be of any help...

matbury
11-28-2007, 01:02 AM
Maybe this isn't what you're looking for but it's possibly a very simple solution:

I remember from using MovieClipLoader in AS 2.0, you just create an empty 'holder' clip that just has a loader and progress script in it. It shouldn't be more that 3 or 4KB.

My basic loader came out at 1KB.

I've been experimenting with more than that though. I'm using a very short, very small looped FLV video file, with transparency, that streams into the preloader to 'entertain' a visitor while the rest loads. You can use a simple SWF file, which streams in automatically too, to get the same effect. It's simpler and easier to do, but an SWF with the equivalent content has a much bigger file size.

The difficulty is getting the video to loop effectively without loading in anything more than the Video class. It's easy with FLVPlayback or VideoPlayer but more of a challenge with just Video!

I've got a very rough demo running here:

http://elearninginteractivekit.com/test_bed/VideoMetadataExample.html

The SWF for this is 2KB. The FLV is somewhere around 475KB. You could make a much smaller animation with vector graphics, of course. This was an experiment to see how far I could push it.

matbury
11-28-2007, 01:07 AM
Here's the document class. I drew the textfield directly on the stage for speed. I'm sure you know how to script a TextField ;)

...and yeah, I know it's messy and it really should be 2 classes!

If it works for you, you owe me $100! :p

package {
import flash.display.Sprite;
import flash.display.Loader;
import flash.events.*;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.URLRequest;
import flash.text.TextField;

public class VideoMetadataExample extends Sprite {
private var videoURL:String = "flv/magic_box_short.flv";
private var url:String = "globe.swf";
private var connection:NetConnection;
private var stream:NetStream;
private var clientObject:Object;
private var video:Video;
private var duration:Number;
public var loader:Loader;

public function VideoMetadataExample() {
connection = new NetConnection();
connection.addEventListener(NetStatusEvent.NET_STA TUS, netStatusHandler);
connection.addEventListener(SecurityErrorEvent.SEC URITY_ERROR, securityErrorHandler);
connection.connect(null);
addEventListener(Event.ENTER_FRAME, enterFrameHandler);

loader = new Loader();
configureListeners(loader.contentLoaderInfo);
var request:URLRequest = new URLRequest(url);
loader.load(request);
//addChild(loader);
}

private function configureListeners(dispatcher:IEventDispatcher):vo id {
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_S TATUS, httpStatusHandler);
dispatcher.addEventListener(Event.INIT, initHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS , progressHandler);
dispatcher.addEventListener(Event.UNLOAD, unloadHandler);
}

// video functions-----------------------------------------------//

private function netStatusHandler(event:NetStatusEvent):void {
switch (event.info.code) {
case "NetConnection.Connect.Success":
connectStream();
break;
case "NetStream.Play.StreamNotFound":
trace("Unable to locate video: " + videoURL);
break;
}
}

private function connectStream():void {
stream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
stream.addEventListener(AsyncErrorEvent.ASYNC_ERRO R, asyncErrorHandler);
video = new Video();
video.attachNetStream(stream);
stream.play(videoURL);
addChild(video);

/*
Store FLV metadata (an Array) in clientObject:Object when
the onMetaData event is triggered. The onMetaData listener
is called directly from the NetStream object independently
of the AS 3.0 event model.
*/
clientObject = new Object();
clientObject.onMetaData = metaDataListener;
stream.client = clientObject;
/*
I don't know much about the NetStream.client property yet.
You're supposed to use get and set methods with it.
*/
}

/*
When the FLV metadata is retrieved, get the duration, width and
height of the FLV stream and adjust the video object to match it.
*/
private function metaDataListener(object:Object):void {
duration = object.duration;

/* Default video size is 320 x 240. If you're using non-standard
FLV sizes, you can match the Video object's size to the NetStream's
*/
var FLVWidth:int = object.width;
var FLVHeight:int = object.height;
video.width = FLVWidth;
video.height = FLVHeight;
video.x = stage.stageWidth / 2 - (FLVWidth / 2);
video.y = stage.stageHeight / 2 - (FLVHeight / 2);
}

private function enterFrameHandler(event:Event):void {
if(stream.time >= duration){
stream.seek(0);
}
}

private function securityErrorHandler(event:SecurityErrorEvent):voi d {
trace("securityErrorHandler: " + event);
}

private function asyncErrorHandler(event:AsyncErrorEvent):void {
// ignore AsyncErrorEvent events.
}

// Loader functions ------------------------------------------------//

private function completeHandler(event:Event):void {
trace("Event.COMPLETE: " + event);
}

private function httpStatusHandler(event:HTTPStatusEvent):void {
trace("HTTPStatusEvent.HTTP_STATUS: " + event);
}

private function initHandler(event:Event):void {
trace("Event.INIT: " + event);
}

private function ioErrorHandler(event:IOErrorEvent):void {
trace("IOErrorEvent.IO_ERROR: " + event);
}

private function openHandler(event:Event):void {
trace("Event.OPEN: " + event);
}

private function progressHandler(event:ProgressEvent):void {
//trace("ProgressEvent.PROGRESS: " + event);
var total:Number = event.bytesTotal;
var loaded:Number = event.bytesLoaded;
progText.text = "Loading... " + Math.floor(loaded / total * 100) + "%";
}

private function unloadHandler(event:Event):void {
trace("Event.UNLOAD: " + event);
}
}
}

atomic
11-28-2007, 01:16 AM
Or why don'y you just do it like the old AS2.0 days?

Don't uncheck anything, and use a seperate preloader movie only, to load the other movie...

Flash Gordon
11-28-2007, 01:40 AM
Hi matbury,

Thanks for the info. There is a basic question I'm trying to get answered. When you uncheck the export in 1st frame, the classes will still work however no graphics that are linked to that class will be displayed. I'm not sure I see how your posts resolved that issue....:o

@atomic, Yea, but that is kind of skirting the issue. We'll call that "plan B", but I got a lot of site that are very design specific so the sperate preloader thing isn't the best solution. Plus, I'd like to know what is going on.

Thanks guys for all the input!

atomic
11-28-2007, 01:44 AM
Where's Senocular when you need him?

Flash Gordon
11-28-2007, 01:46 AM
Where's Senocular when you need him?

http://actionscript.org/forums/showpost.php3?p=655667&postcount=6

last point. :p

But on topic, I'll reiterate: I don't think it can be done. I think AS 3.0 is set up differently. The best you can do is export all classes on frame 1 and uncheck the linkage for export on frame 1. The classes will still export on 1 but library items will export later. This is what I've been doing and I'm around 32KB when a site is done.

atomic
11-28-2007, 01:47 AM
[email protected]

Or...

girlofyourdreamsSenocularalreadyhadsexwith@hotmail .com

She should be able to find him!

Flash Gordon
11-28-2007, 01:50 AM
Thanks for that, but I wouldn't dare email him a Flash question. He visits the form and if he choose to answer that is his business. His personal time is his.

But thanks again for that info :)

atomic
11-28-2007, 01:52 AM
It's on his site...

http://www.senocular.com/contact.php

Flash Gordon
11-28-2007, 03:03 AM
Well, time is running. I think I'm gonna take the question to kirupa. I've give AS[org] over a month :(

atomic
11-28-2007, 04:52 AM
He just even posted tonight, just before your last above post...

http://www.actionscript.org/forums/showpost.php3?p=677999&postcount=29

Flash Gordon
11-28-2007, 05:03 AM
I know......

atomic
11-28-2007, 05:05 AM
You should of mailed him...

Flash Gordon
11-28-2007, 05:08 AM
I figured he's seen the thread. I've been bump it for over a month now. If he didn't want to answer, that's okay. I don't answer every question either so i understand.

dcster2001
11-28-2007, 06:51 AM
The answer according to Moock's Essential ActionScript 3.0 is as follows:



When using the "Export Classes in Frame" compiler option, take note that:

1) A .fla file's document class is always exported in Frame 1, regardless of the specified value for "Export Classes in Frame."

2) All classes referenced by the document class or referenced in a main timeline script are exported in Frame 1, regardless of the specified value for "Export Classes in Frame."

3) If "Automatically declare stage instances" is enabled, and the main timeline's Stage contains named instances of a symbol that is linked to a class, then the linked class will be exported in Frame 1 (because the class is referenced in the automatic variable declarations for the instances on Stage).

I have rewritten your code to include a preloader and dynamically add a Box on frame 15, along with a manually created, unnamed Box instance, also on frame 15. This loads the Box instances on frame 15, and shows the load progress (will be very fast due to file size). I hope this is what you were looking for.

Flash Gordon
11-28-2007, 07:30 AM
Thanks for the example, but that doesn't fix the issue. However, the quotation does confirm what I've already stated.

I'm not sure where this falls in our agreement. Please feel free to give your input as to weither I owe you $150 or not.

Thanks again!
:)

dcster2001
11-28-2007, 11:32 PM
The example I presented was taken directly from Moock's example code, so I figure that's the way you have to do it with AS3 in Flash at this point. There seems to be no other way it works. I hear the developers are working on making AS3 work better with Flash timelines. It seems as though there are problems with assets loading yet not being available because the code load vs read timing is off. I've been frustrated with the same sort of issues.

As for the cash reward, it's up to you whether or not the information answered your question and whether this puts the issue to rest for you.

Flash Gordon
11-28-2007, 11:38 PM
It answered my question, but it doesn't solve the problem. You can't have the assets exporting of frame 1 like you have now. There is no point of a preloader if that is done since the assets are the most of the bulk. And if classes are always on frame 1, that no different than what I current have: 32K in frame 1. It turns out, my issue can't be solved! Only a seperate preloader like atomic mentioned is going to solve it.

Send me a PM if you want to discuss this in private. I want to be fair about it.

dcster2001
11-29-2007, 12:35 AM
The main assets start loading in frame one, that's true, but the movie will display once the first frame content is loaded (you can do this with the Event.INIT listener in a document class .as file as well). The key is having the preloader assets small enough so that the initial display of the movie isn't delayed significantly while the first frame loads... The preloader takes care of the rest.

I am wondering whether the export actually loads the assets or simply starts the loading of the assets. I know that an asset isn't available until it is actually present on the stage. Moock's description of the file states that "the assets are loaded on frame 15."

Flash Gordon
11-29-2007, 12:53 AM
This is a screenshot of the first frame of the preloader you attached when a real asset is added. I simply added a pic instead of a blue book. You can see it all exports on frame 1.

dcster2001
11-29-2007, 01:23 AM
Okay, I think I'm getting it now. Again, from Moock's Essential ActionScript 3.0 (the sky.fla is the example code I modelled the test3.fla after):

Preloading Classes
By default, when the Flash authoring tool compiles a .swf file, it exports all classes in the .swf file's first frame. As a result, all classes used in the .swf file must finish loading before the .swf file's first frame is displayed. Depending on the the total file size of the classes included in the .swf file, this loading process can result in a noticable delay before the .swf file begins to play.

To avoid the class-loading delay, we can export a .swf file's classes after Frame 1 in the main timeline, and then provide a simple timeline script that displays progress information during the class-loading process.

As an example, let's change our sky.fla file so that the classes is uses aren't loaded until frame 15. Note, however, that a .swf file's document class-- and evey class the document class references, whether directly or indirectly-- always loads in Frame 1. Hence, to prevent the Star class from loading before Frame 1, we must first remove the instance names from all manually created Star instances and we will delete the definitions for the variables star1 through star5 from the Star class code. To remove the instance names, we select each Star instance on Stage and delete its name from the properties panel.

The following procedures describe how to load sky.fla's classes at Frame 15:

1. Open the sky.fla in the Flash authoring tool.
2. Choose File -> Publish Settings.
3. In the Publish Settings dialog box, on the Flash tab, next to ActionScript Version:Version 3.0, click Settings.
4. In the ActionScript Settings dialog, for Export Classes in Frame, enter 15.
5. Click OK to confirm the ActionScript Settings.
6. Click OK to confirm the Publish settings.

Next, we add a very basic timeline preloader to sky.fla so that a "Loading" message appears while the classes load...

(Preloader code omitted)

... That's it!

So it is the .swf Publish Settings that controls when classes load, not the individual linkage properties of the class itself that determines when the class is actually loaded. In fact, I can't figure out what the class linkage "Export in Frame 1" actually does, except that you seem to have to check it, since the class will not display unless it is set to export at Frame 1.

So I think the question is answered, and the problem solved as far as I can tell, unless Moock is all wet.

Flash Gordon
11-29-2007, 01:38 AM
Note, however, that a .swf file's document class-- and evey class the document class references, whether directly or indirectly-- always loads in Frame 1.That's the answer, like I even mentioned back in post #21, but it doesn't fix the issue. These classes cause a noticable delay! A true single file .swf preloader is imposssible.


However, this still leaves the issue in the air as to what was happening in the orginal file that I posted where the graphics are never shown.

dcster2001
11-29-2007, 01:53 AM
Nice pic, Flash! That's quite a loaded asset!;)

I am not familiar with the debugger, but doesn't it say that you are on frame 15 (status: frame 15)? Given the code, it would be impossible for the asset to be displayed on frame 1, since there's no code to allow it-- it's all blank frames, except for the preloader dialog.

Again, sorry if my ignorance of the debugger is misinterpreting the data you presented. Could it also be possible that the loading process itself is completed on Frame 1 because the file size is so small.

The preloader is polling the ongoing load operation of the swf that begins on frame 1, but with a 15 frame buffer to allow the movie to load faster. Thus, the loading process begins immediately for the rest of the classes, but the INIT event allows the movie to display frame 1 as soon as it's available. I think I'm now getting the logic of the whole thing:

It wouldn't make sense for a swf not to start the loading process immediately. If that were true, the movie would never get to frame 15, because the loading process would never start and the preloader would loop forever. The Publish Settings "Load Classes" statement allows the movie enough space to create a preloader and display the movie immediately, without waiting for the other loaded assets to load first. Therefore, you have to specify "Export in Frame 1" in your classes' linkage dialog for the preloader to work as intended. Otherwise, you'd have nothing loading until then, defeating the purpose of the preloader and causing an endless loop.

It would be nice if there were an explanation of these nuances somewhere, but again, I don't think Colin Moock would get something like this wrong...

dcster2001
11-29-2007, 02:01 AM
That's the answer, like I even mentioned back in post #21, but it doesn't fix the issue. These classes cause a noticable delay! A true single file .swf preloader is imposssible.


Actually test3.fla doesn't have a document class per se (one is created when the fla is published, but you didn't write a document class .as file controlling the start of the movie, which is what Moock is referring to here).

Are you sure the code in your real movie doesn't invoke a lot of assets in its document class .as file or on the first frame of the fla?

Flash Gordon
11-29-2007, 03:25 AM
Don't worry about it. Unless someone can show me an example of how to preload an OOP swf without have all classes exported in frame 1, this thread won't be solved. And since it isn't possible.....

Anyway, thanks everyone for the input.
Cheers
:)

creynders
11-29-2007, 09:36 AM
Bad news for you flash gordon...
Look at the zip and you'll find it does exactly what you want. It's ugly and hackish, but it does the job.
Now, hand over that cash ;)

First have a look at it and then I'll give you some explanation if you want.

--EDIT: Minor mistake: fixed--

creynders
11-29-2007, 10:13 AM
I won't have time to explain it later on, so I'll do it at once.

The flow is as follows:
The document class of the fla file is FilePreloader.
It gets instantiated, waits to load the full swf file (here's where the progressbar would come if you want to use one)
Then it starts playing and checks whether the playhead has passed the frame where the other classes have been exported to.
If that is the case it instantiates the "Application" class, which is like your main class. Which in turn instantiates the Box class (which has a graphic in it, to test whether my solution is "real" put a 2Mb picture in box and you'll see it's correct)

The EmbedClasses class and Lib symbol is needed, you have to put ALL classes and libsymbols you want to have exported in this symbol and put it on the main timeline on the frame where you defined your classes to be exported to.

You'll notice an "Application" symbol in the library as well (with nothing in it). This may seem useless, but it's necessary because you HAVE TO add it to the EmbedClasses symbol.


BTW, what led me to this solution is this article:
http://kb.adobe.com/selfservice/viewContent.do?externalId=kb401348&sliceId=1

dcster2001
11-29-2007, 03:35 PM
Well I finally loaded the test3.swf to my website and tested it with a 2MB pic attached to Box. You are correct, Flash_Gordon, there is serious lag before the dang thing displays. I am a bit pissed with Moock on this one. His preloader really doesn't work as he purports-- and I don't even know of a way to let him know about it-- grrrrr.

Also, the link provided by creynders seems to suggest that the display bug was fixed, but there's no way to get it to display the dynamic Box instance unless either the .swf Publish Settings is set to load classes on Frame 1 or the linkage settings for Box export on Frame 1, either of which defeat the purpose of the preloader.

This is sort of basic to me-- why would something like this get overlooked by the developers?:( I assume from all this that the Event.INIT listener really doesn't display just the Frame 1 content until all the other classes are loaded as well... this sucks.

creynders
11-29-2007, 03:37 PM
Also, the link provided by creynders seems to suggest that the display bug was fixed, but there's no way to get it to display the dynamic Box instance unless either the .swf Publish Settings is set to load classes on Frame 1 or the linkage settings for Box export on Frame 1, either of which defeat the purpose of the preloader.
Are you talking about the files in the zip file, because therein the box IS exported on frame 10...

dcster2001
11-29-2007, 04:27 PM
creynders, I was referring to the article link you provided, not your code. I see no reason why Flash_Gordon's original file doesn't display the dynamic Box instance, but it doesn't. The article seems to indicate that it should work as he wrote it with the latest version of the Flash player...

Flash Gordon
11-29-2007, 06:14 PM
Bad news for you flash gordon...
Look at the zip and you'll find it does exactly what you want. It's ugly and hackish, but it does the job.
Now, hand over that cash ;)


Knowledge is never bad news, bro. I'll keep my word and pay like I said I would if you want (once I check it out). PM me your PayPal addy if you like. But also I mentioned to PM me first if it was about the money. As you can see, I'm talking to dcster2001 about what we should do. I can't afford to pay 2 people.

Thanks for the help. I'll check out the files a little later and ask any questions. However, it looks like atomic has the best answer for this. It's more of a standard and working with other developers, keeping standards is a good thing. Gezzz.....now I got 3 people to pay. :o

atomic
11-29-2007, 06:25 PM
For you... Anything I contribute will always be for free, as it's always been! :p

Flash Gordon
11-29-2007, 09:43 PM
The EmbedClasses class and Lib symbol is needed, you have to put ALL classes and libsymbols you want to have exported in this symbol and put it on the main timeline on the frame where you defined your classes to be exported to.

I noticed all of your classes extends a DisplayObject. So since it has to be linked with a MovieClip in the library, I guess this means they all have to extend a DisplayObject? It's cool if you don't have time to answer, I understand we're all busy.

But yea, I'm gonna with the separate preloader to load the main movie. It seems like the above approach just kills the organization of the swf.

Cheers guys.
:)

Flash Gordon
11-29-2007, 09:44 PM
For you... Anything I contribute will always be for free, as it's always been! :p
What a guy! Thanks for all the recent help with combo boxes; and still the preloader king. So when you are gonna learn AS 3.0, so you can help with all my other questions too? ;)

atomic
11-29-2007, 09:46 PM
Does that mean I get nothing? Not even a symbolic contribution to my Paypal account? :eek:

Flash Gordon
11-29-2007, 09:47 PM
Does that mean I get nothing? Not even a symbolic contribution to my Paypal account? :eek:

It means I left you off easy over here: http://actionscript.org/forums/showthread.php3?t=153534&page=2

:p

atomic
11-29-2007, 09:48 PM
As for learning AS3.0... As soon as you provide me with a copy of CS3!

I'm almost retired, revenues are down!

Flash Gordon
11-29-2007, 09:50 PM
What you talkin' about? Canadian dollar is 1-1 with US currently. It's a great time to buy it.

atomic
11-29-2007, 09:51 PM
Left me off easy?

Don't get that one!

atomic
11-29-2007, 09:52 PM
Down from 1.10 last week... As my revenues!

creynders
11-30-2007, 09:19 AM
Knowledge is never bad news, bro. I'll keep my word and pay like I said I would if you want (once I check it out). PM me your PayPal addy if you like.
Don't worry man, I've never taken money for giving advice, I won't do now either. :)
And as for the solution... Yeah, it's messy and ugly.
And yes all exported classes need to extend a DisplayObject. So all in all, even though it IS possible to define export to another frame than frame 1, it's so convoluted that a separate preloader is MUCH easier.

Flash Gordon
11-30-2007, 06:47 PM
Cool.Then thanks for the help!

Yea, it is quite messy and it will kill any kind of structure of an application. In my, most of my classes aren't displayobject but logical operations and a lot of inheritance already. I don't think I could make that structure work if I wanted to.

Anyway, I'm very glad this turned into a good discussion and I'm very grateful for everyone posting!

ljones
12-05-2007, 05:32 PM
I've been trying to resolve what appears to be a loading issue. I have a number of clips with linkageIds where I deselected all Export in first frame (EFF) to make a loading animation in the first frame, in order for it to show up. I then noticed that some, but not all, of those clips weren't displaying. I went in and selected EFF on those and they displayed as expected. Now the problem is that my preloader won't show until much later, starting at like 50%. So I went in and deselected the EFF and changed Load order from Bottom Up to Top down, and that seems to have done the job. Can you check that setting in yours and see if that does anything for you?

I don't understand why some would show and others not when deselecting all the EFF.

matbury
12-05-2007, 11:48 PM
I still say there's nothing wrong with a good ol' fashioned 'container' SWF that does all the preloader stuff for you:

/*
SimpleLoader class by Matt Bury 2007 - I'm NOT www.mattbury.com!!!

Constructor:
var simpleLoader:SimpleLoader = new SimpleLoader(url:String, font:String, fontSize:uint, posX:Number, posY:Number);

url is the URL link to the SWF to load e.g. "myMovie.swf"
font is the typeface to display the loading... ??% message to the user e.g. "Arial"
fontSize is the size of the loading... ??% text e.g. 15
posX is the x position of the loading... ??% TextField e.g. stage.stageWidth / 2
posY is the y position of the loading... ??% TextField e.g. stage.stageHeight / 2

e.g. FLA code:

var url:String = "myFile.swf";
var font:String = "Arial";
var fontSize:uint = 15;
var posX:Number = stage.stageWidth / 2;
// always put an object a little higher than the centre to avoid the
// impression that it's 'sliding' down the screen:
var posY:Number = stage.stageHeight / 2 - 20;
var simpleLoader:SimpleLoader = new SimpleLoader(url,font,fontSize,posX,posY);
addChild(simpleLoader);

*/

package {
import flash.display.Sprite;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;

public class SimpleLoader extends Sprite {

private var _loader:Loader;
private var _loading:TextField;
private var _font:String;
private var _fontSize:uint;
private var _posX:Number;
private var _posY:Number;

// Set private variables passed in from FLA and load the file
public function SimpleLoader(url:String, font:String, fontSize:uint, posX:Number, posY:Number) {
_font = font;
_fontSize = fontSize;
_posX = posX;
_posY = posY;

_loader = new Loader();

// Event listeners keep track of the download:
_loader.contentLoaderInfo.addEventListener(Event.O PEN, openHandler);
_loader.contentLoaderInfo.addEventListener(Progres sEvent.PROGRESS, progressHandler);
_loader.contentLoaderInfo.addEventListener(IOError Event.IO_ERROR, ioErrorHandler);
_loader.contentLoaderInfo.addEventListener(Event.C OMPLETE, completeHandler);

var request:URLRequest = new URLRequest(url);
_loader.load(request);
//If you want the SWF to 'stream in'
// i.e. play the timeline while it's loading:
//addChild(_loader);
}

// Create a TextField to display the download progress
private function openHandler(event:Event):void {
var _format:TextFormat = new TextFormat();
_format.font = _font;
_format.size = _fontSize;
_format.bold = true;

_loading = new TextField();
_loading.autoSize = TextFieldAutoSize.CENTER;
_loading.defaultTextFormat = _format;
_loading.x = _posX;
_loading.y = _posY;
_loading.text = "Loading... ";
addChild(_loading);
}

// Display the download progress
private function progressHandler(event:ProgressEvent):void {
var percent:uint = Math.floor(event.bytesLoaded / event.bytesTotal * 100);
_loading.text = "Loading... " + percent + "%";
}

// Always handle ioError events
private function ioErrorHandler(event:IOErrorEvent):void {
_loading.text = "Network failure. Cannot load file. Please try again later.";
}

// Clean up and show loaded SWF
private function completeHandler(event:Event):void {
addChild(_loader); // doesn't show SWF until it's fully loaded
removeChild(_loading);
// I don't know if this is necessary but it marks the following
// objects for garbage collection as they're no longer needed
_loading = null;
_font = null;
}
}
}

If you want something 'Flashier' (no pun intended) you could create a timeline only animated SWF that loads in first (very quickly if it's vector graphics only) to entertain your users while it loads.

I sometimes use Swift 3D which exports vector SWFs and FLVs among other things (FLVs work better for bitmap animations with a lot of detail because they're smaller but they're more difficult to loop when you're using only the Video class).

ljones
12-06-2007, 12:32 AM
I must say that I neglected to remove a clip that had the linkage clips contained in it, I thought that was odd. And the ones that were not displaying were not in the 'linkage' clip.

I would agree that there is nothing wrong with a loader clip in another movie, however in my case I am working on someone else's file that references the '_root' throughout.

matbury
12-06-2007, 12:55 AM
How are they doing that? I though _root had been depricated. The nearest thing to it is the Stage class - stage

ljones
12-06-2007, 12:56 AM
As 2.0

matbury
12-06-2007, 04:01 PM
Ah, that threw me. And here I was thinking in AS 3.0.

adeptsoftware
12-17-2007, 11:50 PM
Wow after 4 hours on this I'm really sick of it and I'm pretty sure this is an Adobe bug. And a bad one - this should never have gotten out of QA. I'm going to ignore it and hope it's fixed by the time my project is done.

I would like to distribute the preloading - while an introduction plays, load the menu classes & resources, while the menu is displayed load the main game classes & resources, etc. At least loading the classes as a lump anywhere other than frame 1 would be nice.

I hope someone will post here if this gets fixed or if a workaround is found. I've tried every workaround I could think of and nothing works.

fentunfont
12-18-2007, 03:02 AM
lol who wants the US peso these days?

madmac66
03-20-2008, 01:13 AM
I came across this thread searching for 'preload class' as I was getting the typical lag before the frame 1 preloader showed up (at about 50%). I was excited to say the least when I happened upon this thread and found I could export classes at frame 2. This caused my prelaoder to behave exactly as expected. I was happy.

Then as i progressed through my presentation, it turns out my FLVs would not load. Setting classes to export on frame 1 again and the FLVs were back to normal.

Reading more of this thread it sounds like there is a pretty severe bug with this nonsense. Is this what is causing my file act whacky, or is there something i can do to rectify it. My presentation is way simpler than some of the examples mentioned below.

The preloader is a simple vector box with this code:

onClipEvent (enterFrame) {
loading = _parent.getBytesLoaded();
total = _parent.getBytesTotal();
if (percent == undefined) percent = 0;
percent -= (percent-((loading/total)*100))*.25;
per = int(percent);
this.percentage = per+"%";
//loadBar._width = per;
this.gotoAndStop(per);
if (percent>99) {
_parent.play();
}
}


The second frame loads these classes:

import flash.geom.ColorTransform;
import flash.geom.Transform;
import mx.transitions.Tween;
import mx.transitions.easing.*;


While the third frame has the first FLVPlayback Component and nothing else, no script, no assets. When I click the btn on frame 2 to move to frame 3, I get a blank screen, no FLV. Could there be something else interfering. I have a lot of other code on frame 2 but I dont want to clog up this page with.

Please if anyone has found a solution for this I would be very grateful.
A link to the SWF on our server is here if you feel so inclined, http://dev.binaryinteractive.com/bluetooth/web_exp/

mm66

adeptsoftware
03-25-2008, 02:52 PM
OK I've found a solution! I wrote up a blog post on it here:

Writing a Preloader in Actionscript 3 (http://www.jetpackhq.com/blog/2008/03/25/writing-a-preloader-in-actionscript-3/)

creynders
03-27-2008, 08:22 AM
OK I've found a solution! I wrote up a blog post on it here:

Writing a Preloader in Actionscript 3 (http://www.jetpackhq.com/blog/2008/03/25/writing-a-preloader-in-actionscript-3/)

This certainly is a solution, but if you're switching to flex to do the preloading, you might just as well create a separate preloader in flash or use a module component in flex. In all practical sense it's still a very convoluted and disorganized fix to a problem that shouldn't exist.
But kudos for working it out!

BTW, there's no difference between Flex-AS and Flash-AS. It's exactly the same.
The difference is, that in flex you have a lot more pre-made components and classes. And that it complementary uses a wholly different declarative language altogether: mxml.

adeptsoftware
03-28-2008, 01:04 PM
BTW, there's no difference between Flex-AS and Flash-AS. It's exactly the same.
The difference is, that in flex you have a lot more pre-made components and classes. And that it complementary uses a wholly different declarative language altogether: mxml.

The preprocessor statements like [embed] make it different.. those generate an error if used in Flash, so the languages are not fully compatible. I've noticed a lot of people getting started with Flash trying to use them because they grabbed the wrong docs from Adobe (myself included). Adobe seems really disorganized. If their product didn't have 99% market share, nobody would use it. I sure as hell wouldn't! On the other hand, if they got their act together and simplified things to lower the barrier to entry, there would be more competition for me.

creynders
03-28-2008, 03:16 PM
The preprocessor statements like [embed] make it different.. I'm nitpicking, but I'd say those statements are part of the mxml language, not actionscript. Just like you can write php code in between your html code.

ultramedia
12-03-2008, 04:19 AM
getting the preloader to actually run is only half my problem.

In my size report, frame 1 is 98kb for the AS3 class export, but my client has a requirement that no load (between "screens") can take more than 10 seconds on a 56k modem, 98kb takes about 26 seconds :(

How can I find out what is actually in the 98k bloat so I can figure out how to get rid of it? P.S. my embedded text is about 20k, and my AS3 code is about 4.5k (I did a size report on them in a blank movie).

dcster2001
12-03-2008, 12:54 PM
Preloading in Flash AS3 is not as straightforward as it used to be. For a good tutorial, try out "Preloading in Actionscript 3" at http://www.gotoandlearn.com.

Hope that helps.

matbury
12-03-2008, 01:38 PM
I agree with dcster2001. I've used the seperate preloader SWF technique since AS 2.0. I've tried other techniques but none were as stable and simple to use as this one. If you're good with PHP or some other server-side scripting,you can even create a single preloader SWF to use with all your 'content' SWFs. Just set:

stage.scaleMode = StageScaleMode.NO_SCALE; // don't scale the contents with the Flash Player window
stage.align = StageAlign.TOP_LEFT; // stop the contents from 'edging' their way up and out of the Flash Player window when the browser is resized

And then in the HTML embed code you can adjust the width and height of the Flash Player window according to the size of the 'content' SWF.

You can pass in the URL to the 'content' SWF through FlashVars and reference it in the preloader SWF like this:
var flashvars = {};
flashvars.swfurl = "../path/to/swf/file.swf";
this.loaderInfo.parameters.swfurl;

Please note: This HTML example is using SWFObject 2.0

The other good thing about this technique is that you can also load AS 2.0 SWFs into an AS 3.0 container, although you can't get them to communicate with each other directly. But that's another story...