Adding a Preloader in CS5 as3 project - actionscript-3

I am Working on a cs5 as3 project and in that project i have writen all code in the frame1 of main timeline (there is no package/class in the code)
The code in the main timeline is very complex in which i am loading external images and xml's and many more things .
i have not written any code in the action layer.
Now i want to add Preloader .
how can i add Preloader in this situation ?
here is a snapshot of the timeline :

Move everything to the second frame, and add your preloader code/content on the first frame
http://www.google.ca/search?q=preloader+code+as3

The real issue here is the number and size of external assets you're loading, it may have been worth considering loading some of these assets separately. In this way, your preloader would have been more accurate. However complex your code is , the loading of the assets is the main element to delay the start of your application.
With your current configuration , if you'd encapsulate the loading of some of the assets (as you may not need all of them right from the start) , you would have been able to directly create a preloader within the code as is , without a DocumentClass, simply by loading assets as your first function and calling an init() method after the loading is complete.

Related

Internal AS3 preloader & stage issues

I need to create a single SWF with no external files, so I'm trying to add an internal preloader to my Flash project which has [embed] assets. I know [embed] causes problems with preloaders because it puts the assets on frame 1. I have tried the solutions recommended in these posts, where you set the document class to your preloader class:
Preloader for SWF with embed bytearray
How to create Preloader in AS3
I can get it to work, but ONLY if I comment out any lines of code that involve the stage, otherwise I get an "Error #1009: Cannot access a property or method of a null object reference." Those lines are essential though, so does anyone know how to fix those errors with the stage?
You haven't posted your code or your fla, so all I can do is share what works for me.
First, I wouldn't use Embed. Instead, use a swc. I have found that Embed can be unreliable as far as actually getting the entire asset in there (at least when publishing with Flash Builder + Flash Pro, which is my workflow).
Once you have your assets in a swc, try the following steps:
Set your export frame to Frame 10 (or any frame other than 1--I like frame 10 because then you can read the label that says "Preloader")
Put your actual content on frame 11. You can structure this a lot of ways. Since I program to Interfaces, I give whatever is on frame 11 an instance name and then use a setter to determine that my "first thing" has been placed on stage. I can get away with this because my main Document Class just knows the definition of the Interface, not the full implementation of the Class, so the Class does not need to load for the main Document Class to work. You probably aren't truly using the timeline and probably didn't program to interfaces, so you'll probably just set the base class of the symbol that's on frame 11 to the main logic of whatever you're trying to do.
Put your preloader graphics in Frame 1. I'm not sure why your stage references are so important. I, personally, don't use any logic in the preloader. Instead, I use a spinner that spans frames 1-10 (plus the word "Loading...". The spinner just spins while the classes load. The embed frame acts as a temporary "stop" that just holds the timeline back until those classes have been loaded. Once the classes have been loaded, the timeline will act like you called play() on it. So it really can be that simple. If you need it to be more complicated, give one of your preloader graphics an instance name and set up a getter/setter pair for it, then use the setter to trigger your logic that accesses the stage. You are pretty much guaranteed to have a valid stage at that point.
Word of warning: if you did make use of the timelime, you will get strange results if you try to jump to a frame that isn't loaded yet, so make sure to check to see if a given frame is loaded if it's near the end of your main timeline and your main timeline is heavy with assets before calling goToAndPlay() or goToAndStop().
Some references that might help you further:
Preloaders vs as 3 (I'd recommend you read the entire series this is part of. This is an amazing series I wish I'd found 3 years ago)
Solving the Frame 2 Problem Presentation and code
Combining the Timeline with OOP The example code for that is here (long story)

FlashBuilder: how to embed graphics for preloader?

I develop a project in FlashBuilder. The graphics and UI elements i create in the Flash IDE and give them Export Classnames. then I put the SWC in the library paths of the FlashBuilder project and create the UI elements by instanciation.
Now I want to add a Preloader for the application. I follow this article, which works:
http://pixelpaton.com/?p=4642
My question is now: I also need some graphics for the preloader. But how do I ensure that the graphics for the preloader will be loaded first, such that the preloader class can start as soon as possible?
The compiler will figure out the dependencies for your preloader class, and load them first.
For example:
public function Preloader()
{
addChild(new UIElementFromFlash());
}
Flash Builder will know UIElementFromFlash needs to be loaded before Preloader. It will also load Preloader before your main class and its dependencies as long as you have the Frame metadata tag from the article.
I'd suggest to make 2 swf files if it's possible. Make a loader.swf which loads your application swf and displays the progress and your loading animation/graphics.
See an example here: http://www.republicofcode.com/tutorials/flash/as3preloader/
Then you'll have to add URLLoader class and load your application.swf.
Try looking here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLLoader.html
Like Gio suggested, having two SWF files is the best practice.
However, one other solution is to use the -frame two argument of the Flex compiler. All your definitions (code and assets) will be added to the second frame of the movie, allowing your main class to listen for the loading progress of the application and display animations.
Be careful to not reference anything from the main application in your first frame, to prevent adding more weight to it.
More detail on this technique here:
http://www.andymoore.ca/2009/08/flexsdk-3-3-how-to-make-a-flash-preloader-in-as3/

AS3: Preloader Not Working w/ Exported for AS Graphics

I have a lot of graphics and sounds in my library that are exported for Actionscript in Frame 1. I believe that because of the exported graphics, my preloader will not work. All I get is a white frame while the movie loads.
How can you preload graphics that are exported for Actionscript?
When you export for Actionscript in Frame 1, everything that is exported must be loaded before any content is visible. I usually uncheck this button, and then everything is exported to the first frame where it's referenced. For more details on this approach, check out http://www.developria.com/2010/04/combining-the-timeline-with-oo.html . Howwever, if you're like most developers, you probably reference everything from your main document Class, so this may not do you much good. It does offer the possibility that you can avoid a preloader altogether, though, because loading is spread across different frames.
If your movie is structured like normal, check out http://www.8bitrocket.com/2008/4/22/Tutorial-Preloading-Actionscript-3-AS3-Games-in-Flash-CS3/ .
It is not applicable on every case, but maybe you can preloader in separate file, which loads your main swf.
You can see detailed how to in this tutorial: http://www.gotoandlearn.com/play.php?id=85

Preloader in as3 using preloader sample provided in cs5

I am working on a project in which i want to add preloader and for that i have used the sample preloader provided in cs5 (in sample files). i have moved(from another fla) all my code to second frame and all library content to current fla's library.
Now the problem is that the size of first frame is now 182kb (the size of library content)
which is only 8kb when i have not added my library content in current fla's library.
And due to this when i test the movie the first frame(preloader) is not shown up until the 182 kb is loaded and immediately after that second frame is shown up.
here my question is how can i load library contents in second frame ?
i am having a large number of library content which is expected to increase in future.
here are two snapshot:
the library:
testing the movie:
you can see in second snapshot the size of first frame is 182kb.
Please provide a good solution for this problem.
Thanks !
Assets which have not linkage load until the frame they first appear, so if you move all your assets to the second (or later) frame, you could have code in the first frame that executes before all the other assets (in frame two or later) finish loading.
You could create your preloader in another fla. This new fla's only job would be to load/preload your main fla, so your preloader's fla size would be very small.
In such case, you wouldn't have to move your library content to the second frame of your main fla, quite the opposite actually.

AS3 - How to handle assets during preload

I'm having some difficulties with preloader and assets.
I'm working with FlashDevelop so pure AS3/Flex, no FLASH IDE
The thing is : when I try to addChild() something like a swf or event a simple png/jpg it kills the preloader completly (blanck screen until the game is fully loaded, then the preloader is launch (but the game is already loaded) so one frame later I'm after the preloader.
I think I missed something in the logic behind the management of the famous "first frame" and the loader progress event.
If someone have any idea about what I'm talking about. I'd like some light about the subject ^^
Thx !
-UPDATE-
I re-searched for more informations and found this post :
How does the preloader work in as3?
MichaelJW points my problem but doesn't answer it :
"You can do whatever you like in a preloader, but it won't run any of its code until everything needed by the preloader has downloaded. So if you make a preloader with a 3MB image file and a progress bar, the progress bar won't do anything until the entire image has downloaded!"
So how do you get to use some image to build a preloader ?
I have encountered this before and my "work around" was to build 2 separate loaders. The first is VERY light weight and loads all initial assets (like a rotating 5k png), the second if the "bulk load" of assets etc..
when the first is done loading it triggers the load of the second round of assets. You are right tho, you CANNOT use assets till they are loaded completely..