Preloader actionscript 3
Today if your flash exceed 700 KB (the size of a larger picture), is if you put a Preloader.
Actionscript 3 is very simple in the first frame put the code
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
this.loaderInfo.addEventListener(Event.COMPLETE, onComplete);
function onProgress(e:ProgressEvent):void{
var loaded:Number = e.target.bytesLoaded;
var total:Number = e.target.bytesTotal;
var pct:Number = loaded/total;
loaderMc.barMc.scaleX = pct;
}
function onComplete(e:Event){
play();
}
Where "loaderMc is a MovieClip that contains another MovieClip" barMc. To make things interesting, "barMc" you put on a mask layer in which you can draw like you want to show your progress bar.
Circular Dynamic Preloader Tutorial
Circular Dynamic Preloader Tutorial - Flash ActionScript 3.0 Website Content Loader
Create a simple flash preloader
If you use flash to create web animations or websites then you really need to know how to make and add a preloader to it. Everyone doesn’t have a superfast internet connection and adding some information that there is something loading keeps the viewers stay for it. In this tutorial I’m going to show you just how to create a simple preloader.
Step 1
Open up the animation you wish to add a preloader to. I’m going to use an animation I have for my podcast intro.
Step 2
Open the Scene panel by going under Window > Other Panels > Scene.
Step 3
Here you will see that there are already a scene present with your animation, it’s currently called Scene 1. Double click the name to rename it to ”animation”. Click the little plus icon (circled in red in the figure above) to add a new scene. Name this ”preloader” (without the quotes, all in lowercase). Then after doing that, click, hold and drag it above animation scene to make it load before the animation, which is what we want for the preloader.
Step 4
Now that we have our new preloader scene set up properly, click on it to get working in it. If you have any backgrounds in your other animation that you wish to have in the preloader, add them in again. However be aware that it should be as lightweight as possible!
Step 5
Now we are going to be designing the main preloader. Start by making a new layer and call it ”loading graphic”. After that, grab the text tool (T) and add the text ”Loading” or similar to your page and position it where you want. The properties box should have the type set to static (as seen in the graphic above).
Next, we are going to be adding the text box that will make the loading percentage appear. Create a new layer above the loading graphic layer and name it ”percent”. Grab the text tool (T) and where you want it (probably below the loading text) type the text 100 %. The reason for typing 100 % is pretty simple. It is the longest you will have to go, so you can now style it to your liking. Finally, change the text type from Static Text to Dynamic Text and remove the text you had entered before and scale the box as wide as needed.
Step 7
To be able to control the input in the dynamic text field we just created later with actionscript, we need to give it an instance name. So in the box highlighted in red above, type in ”loadingPercent” (without the quotes, but with a capital P).
Now it is time to put all of this together and create the loader with a little bit of actionscript. Create a new layer and call it ”actions”. We also need to expand the timeline because we want an action to happen after we’ve loaded it all. So click on the second frame on the actions layer and press F6 to add a new keyframe. Next click on frame two on the percent layer and shift-click the one on the loading graphic layer. Then press F5 to create a new normal frame there. Your layers panel should now look like the image below.
Step 9
Click back on frame one on the actions layer and bring up the actions window, either by going to Window > Actions or by pressing Option + F9 (PC: Alt + F9). In the actions panel, type the following code:
percent = Math.floor(getBytesLoaded()/getBytesTotal()*100);
loadingPercent.text = percent + " %";
What this code will do is calculate percent, based on the amount of bytes that are loaded, divided by the amount of total bytes of the file, multiplied by 100. Then on the second line it is saying that the variable loadingPercent (which is what we caled the dynamic text field above) is text and that it should have the output of what you get from the percent calculation at the first line and add the percent sign at the end. Relatively simple.
Step 10
Now we do have our basic preloader and we can go ahead and press Command + Enter (PC: Ctrl + Enter) to preview our movie. What you will notice though is that it doesn’t take us to the animation once it has loaded. Well, that’s because we haven’t told it to.
So click on frame two of the actions layer and again bring up the actions panel. Paste the following lines of code in there:
if (percent ==100){
gotoAndPlay("animation",1);
}else{
gotoAndPlay(1);
}
To break this down. It is a rather simple if/else statement. What it does is say if the value of percent equals 100 then goto and play the scene names animation and start at frame 1. If percent doesn’t equal 100, then go back and play frame one of this scene.
Step 11
Now that we have added this code we can go ahead and test our movie again and the preloader should load and after take us to the animation.
This is really a simple preloader that could be animated a bit further but just remember to keep it lightweight. People don’t want to wait for the preloader or to preload the preloader.
Create Basic Flash Preloader
In this tutorial, I’m going to show you how to build a preloader. You should have your own preloader in minutes!
- Create a new document.
- Name the first layer to “loader bar”
- Draw a line. This line will be our preloader bar.
- Convert the line into a movie clip. Set the registration point to the top left corner.
- Give the line an instance name of “preloaderBar”.
- Create a new layer called “actions”
- Type the following in the “actions” layer
preloaderBar.scaleX = 0;
var loader = new Loader();
loader.load(new URLRequest("main.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
addChild(loader);
Explanation: First we set our preloader’s X scale to zero. This way, the user can’t see the preloader at the beginning of the loading process. Then, we create a Loader object. The Loader object is used to load SWF files or image files. After the Loader is created, we tell it load our main movie (main.swf). Then comes the important part, we assign event handlers for the loader. To be more specific, we are using the “contentLoaderInfo” to access loading progress information and statistics. In this case, we want to know when the loading is still in progress (progressHandler) and when the loading is complete (onCompleteHandler). Last, we add the loader to the stage.
NOTE: You must have the “main.swf” file in the same directory as this loader file!
8. Now that we have our loader ready, we can write the functions that are responsible for animating the preloader. Type the following
function onCompleteHandler(e:Event):void {
preloaderBar.visible = false;
}
function progressHandler(e:ProgressEvent):void {
var per = e.bytesLoaded/e.bytesTotal;
preloaderBar.scaleX = per;
loader.visible = true;
}
Explanation: The “onCompleteHandler” is called when the loading is done. In this case, we don’t want to show the user our preloader anymore, so we make it invisible. The “progressHandler” is called everytime the loading progresses. Here, we simply calculate the percentage that has been loaded (loaded/total size). Then, we assign into the preloader’s X scale property the precentage value. So when the percentage is 1 (loading is done), the “preloaderBar” will be at full width (scaleX = 1).
9. Test your movie!
NOTE: You can simulate a download in the Flash IDE. Test your movie and go to View-> Simulate Download.
Create a Flash Preloader
Creating a Flash Preloader.
To keep visitors waiting for your animation to load you should always display a preloader and advise users how long they will have to wait. In Flash you have to program the preloader manually using getBytesLoaded, getBytesTotal and gotoAndPlay functions. Fortunately Flash Designer does the ActionScript part for you, all you have to do is to put a nice "progress" animation on the preloader frame.
You can create your own animation or use one of stock preloader clips ready to insert on the preloader frame, like the one below (the actual animation doesn't load for demonstation purposes):
To customize the preloader:
- Open or create your animation
- Choose "Movie" > "Edit Preloader"
- Choose "Movie" > "Insert Standard Preloader" and choose one of the clips. Click OK.
- Press Shitf + F9 to preview the preloader
- Export SWF file and the web page, choose "File" > "Export Web Page".
- Upload SWF and HTML file to your web site.
- Launch the browser and navigate to the page you've just uploaded to watch the preloader loading your animation.
NOTE: The preloader will display only when the animation takes some time to load. This happens only when you download the flash animation over the internet connection and only for the first time. When you view the file locally (for example when you press "Play Animation") the preloader doesn't appear because the animation loads at once, and the preloader has no chance to start showing the progress.
To test the preloader, publish your file on the web site. To display the preloader again you have to clear the cache of your browser.



