Forum Topic: AS3: Preloader

(314 views • 0 replies)

This topic is 1 page long.

<< < > >>
Kissing

chunkycheese12

Reply To Post Reply & Quote

Posted at: 9/9/08 11:35 AM

chunkycheese12 LIGHT LEVEL 15

Sign-Up: 11/05/07

Posts: 1,265

AS3: Main

AS3: Preloader

I've been beginning to start learning AS3 lately, and I thought I was ready to make a tutorial on preloaders(One of the only things I've had a lot of success with). So lets get started. Begin by making the framerate 30FPS. Now make a rectangle, making sure that the width is 300(the height doesn't matter). Select ONLY the inside , and press F8 on your keyboard. Give it a name of 'loader', and make sure the registration point is in the top left. Make sure that It's a Movie Clip, then press Ok. Go to the properties panel and give it and instance name of 'loadingBar' (remove the quotes, obviously). Now create a dynamic text, and give it an instance name of 'loadingText' (once again, REMOVE QUOTES!).

Now for the code. Select the first frame, and press F9. The actions panel should pop up. Give it the following code:

stop();//Stops the movie
loaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);//LoaderInfo dispatches ProgressEvent.PROGRESS events, so we registered a listener with that object.
//ProgressEvent.PROGRESS indicates that a new portion of the movie has arrived and gives us the latest load-progress information.
loaderInfo.addEventListener(Event.COMPLETE, completeListener);//Event.COMPLETE indicates that the swf has completely downloaded,which also needs a listener registered.
function progressListener(e:ProgressEvent):void {//progressListener() first divides the number of bytes that have been received so far (bytesLoaded), by the size of the swf in bytes (bytesTotal), and stores the result in loaded(on the line below).
  var loaded:Number = e.bytesLoaded / e.bytesTotal;//. If half of the swf had downloaded, then loaded would be 0.5.
  var percent:int = loaded * 100;//To get the load-progress as a percentage, loaded is multiplied by 100, the result is automatically converted to an int and stored in percent
   loadingText.text = "Loading: " + percent + "%";//The text field is updated to show the current load-progress as a percentage
  loadingBar.scaleX = loaded;//he scaleX property of the loading bar is set to loaded. If loaded were 0.4, the loading bar would be set to 40% its original length.
}//Closes the progressListener function.
function completeListener(e:Event):void {//This creates a function called completeListener, which will happen when the SWF has fully loaded.
  loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progressListener);//This listener unregisters itself and progressListener(), before instructing the swf to go to and stop on frame 2. This is usually where the game would begin(this bit explains this line and the next 2, btw).
  loaderInfo.removeEventListener(Event.COMPLETE, completeListener);
  gotoAndStop(2);//Goes to and stops on frame 2.
}//Ends the completeListener function

Phew, I think that's all of it. The whole code is explained in the comments on the code, so READ THEM AND UNDERSTAND THEM!!! I hope you learned something today. You should end up with something like this:
http://spamtheweb.com/ul/u
upload/090908/59601_AS3preloader.php

Ignore the picture, it was big...ish so I could show that the preloader worked. Oh, and i hope that <cod > (I removed the e for a reason :P) works, it might look a bit weird if it doesn't.

I LIEK EESTAH - Easter Collab '09

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 08:12 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!