Forum Topic: As3: Simple Preloader, Step By Step

(17,410 views • 37 replies)

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
Thinking

Denvish

Reply To Post Reply & Quote

Posted at: 5/10/07 06:04 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

AS3: Main

AS3: Simple Preloader, Step By Step

Step 1

Create your movieclip and textfield. For this recipe, you'll need a simple rectangle, and a dynamic textfield

a) Draw your rectangle, to the width you wish it to be when fully loaded. Remove the outline by double-clicking on it and pressing delete.
b) Select the rectangle, and right-click>Convert to Symbol. Select Movie Clip, and give it a name like loadBar. Click OK.
c) With your new MC still selected, use the property bar (CTRL+F3) to give it an Instance Name. My example uses lbar
d) If you wish your bar to load from left to right, as opposed to outwards in both directions from the middle, follow steps e - g
e) Double-click your new MC to enter Edit mode
f) Select the rectangle, and drag it so that the left-hand end is on the little cross (MC registration point, 0,0).
g) Exit Edit mode by double-clicking anywhere except the rectangle

Now for the textfield. Back on the main Stage

a) Select the Text tool (T), and in the Properties bar, select 'Dynamic Text' from the dropdown.
b) Drag yourself a textbox at the desired position above or below the loadbar
c) Select it, and in the Properties Bar, give it a name for reference. My example uses lpc
d) If you want it to look professional, click the 'Embed' button on the Properties bar, select 'Numerals' from the list, and type % in the 'include these characters' box below. Click OK to close the embed window

OK. On to the Actionscript. Select frame 1 on the main timeline, and add this code:

//Import the required assets
import flash.display.*;
//Stop the playhead while loading occurs
this.stop();

//Create a listener to call the loading function as the movie loads
this.loaderInfo.addEventListener (ProgressEvent.PROGRESS, PL_LOADING);

/*This is the main function, basically it grabs the total and loaded bytes,
calculates a percentage, and displays it by stretching the bar and adjusting
the textfield display. If your bar/textbox instancenames are NOT lbar/lpc,
you'll need to adjust this code to match your instance names*/

function PL_LOADING(event:ProgressEvent):void {
var pcent:Number=event.bytesLoaded/event.bytesTot al*100;

//Stretch the bar
lbar.scaleX=pcent/100;
//Display the % loaded in textfield
lpc.text=int(pcent)+"%";
//If the movie is fully loaded, kick to the next frame on the main timeline
//You may wish to change the gotoAndStop(2) to a gotoAndPlay(2)

if(pcent==100){
this.gotoAndStop(2);
}
}

That's it. Just start your movie on frame 1, rather than frame 2, on the main timeline. If you've already made your movie, you can shift all frames by selecting the first frame, SHIFT-Clicking the last frame, and dragging them all to the right by one frame (yeah, I'm sure most of you know this already, but there's no harm in mentioning it)

You can test your preloader offline, by pressing CTRL+ENTER twice. While in that mode, you can use the View>Download Settings to simulate download speeds ranging from 14.4 kbps modem, up to a T1 line.

If you don't want to spend the time learning this or creating the necessary assets, there are two versions of the source. Version as of this tut is here, and there's also a version with a Play button on the second frame over there.

If you're interested in expanding further, or adding more glitz to your preloader, consider using the pcent/100 to manipulate the playhead in a 100-frame MC. It's also worth checking out the other triggers for ProgressEvent in the Flash Help files, for more information on the subject.

Feel free to add your own approach to preloader to this thread

- - Flash - Music - Images - -

BBS Signature

None

Vengeance

Reply To Post Reply & Quote

Posted at: 5/10/07 07:20 PM

Vengeance EVIL LEVEL 28

Sign-Up: 03/18/05

Posts: 5,035

Very good tutorial. Everything explained well and easy to understand.

========|| WWWWWWWW>[-[Blog] - [Audio] - [Userpage] - [Flash] - [Last.fm]-]<WWWWWWWW ||========

BBS Signature

None

atomic-noodle

Reply To Post Reply & Quote

Posted at: 5/10/07 07:34 PM

atomic-noodle NEUTRAL LEVEL 14

Sign-Up: 05/17/05

Posts: 1,610

Awesome, Thanks :D

iamcoreyg.com // campnorth
Need a website? music? graphics? CONTACT ME.

BBS Signature

None

UnknownFear

Reply To Post Reply & Quote

Posted at: 5/10/07 08:06 PM

UnknownFear FAB LEVEL 34

Sign-Up: 02/27/04

Posts: 6,912

Nice job, Denvish. Will look into this tutorial and a few of the AS3 tutorials in the AS3: Main when I get my copy of Flash 9.

Blog
[ T | F | G/MSN ]
--Ubuntu 9.10 "Karmic Koala" User--


None

Battered-Prawn

Reply To Post Reply & Quote

Posted at: 5/11/07 12:02 PM

Battered-Prawn NEUTRAL LEVEL 12

Sign-Up: 08/02/06

Posts: 1,170

Thanks so much m8!

have you got any advice to someone (me) who has no knowledge of AS and wants to learn AS3? or any tuts to get me started?

Why not send me a PM, or visit my User Page

BBS Signature

None

Battered-Prawn

Reply To Post Reply & Quote

Posted at: 5/11/07 12:06 PM

Battered-Prawn NEUTRAL LEVEL 12

Sign-Up: 08/02/06

Posts: 1,170

ahh, forgot to mention, is tehre anyway for it to auto play after it loads?

Why not send me a PM, or visit my User Page

BBS Signature

None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 5/11/07 12:08 PM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,370

At 5/11/07 12:02 PM, Battered-Prawn wrote: have you got any advice to someone (me) who has no knowledge of AS and wants to learn AS3? or any tuts to get me started?

I assume you don't know other languages too?
Do lots of experiments to develop an ability to think for yourself. People who don't take off from using stepbystep tutorials only always complain about how stuff is insufficiently covered for them.
But anyway, there aren't many stepbystep tutorials for AS3 so you might be lucky. :P
Most AS2 stuff applies to AS3 as it is not that diffirent though, you just gotta know what becomes what.

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

Thomas

Reply To Post Reply & Quote

Posted at: 5/11/07 12:15 PM

Thomas LIGHT LEVEL 13

Sign-Up: 02/14/05

Posts: 2,833

At 5/11/07 12:06 PM, Battered-Prawn wrote: ahh, forgot to mention, is tehre anyway for it to auto play after it loads?

Well if you have a full preloader MovieClip,you could add _root.play(); to the last frame of your MC.You could also have it play from the AS(I think)

Try:
if(pcent==100){
_root.play();
}


None

Battered-Prawn

Reply To Post Reply & Quote

Posted at: 5/11/07 12:15 PM

Battered-Prawn NEUTRAL LEVEL 12

Sign-Up: 08/02/06

Posts: 1,170

ok, so basically analyse the code and see what i can come up with. thanks.

*ignore my second post*

Why not send me a PM, or visit my User Page

BBS Signature

Happy

Depredation

Reply To Post Reply & Quote

Posted at: 5/11/07 03:12 PM

Depredation LIGHT LEVEL 17

Sign-Up: 09/05/05

Posts: 4,781

Thanks for this. AS3 is actually a hell of a lot easier than i thought, i am starting to get the hang of it now :).

BBS Signature

None

BlueHippo

Reply To Post Reply & Quote

Posted at: 5/11/07 03:22 PM

BlueHippo LIGHT LEVEL 40

Sign-Up: 07/25/04

Posts: 10,859

mmmm, that's a nice tutorial. any brainless slug should be able to figure that out... which reminds me, i've been using weebl's preloader (i stole it when he gave me a .fla to work on one of his weebl and bob episodes, i'm a whore :( ) for ages, i think it's time i made my own :P

thanks denvish <3

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - : : fart : : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

BBS Signature

None

ShittyFlash

Reply To Post Reply & Quote

Posted at: 5/11/07 05:28 PM

ShittyFlash EVIL LEVEL 05

Sign-Up: 04/13/07

Posts: 62

Thanks alot i really needed it.


None

Cluelessdolphin

Reply To Post Reply & Quote

Posted at: 7/7/07 12:41 PM

Cluelessdolphin NEUTRAL LEVEL 01

Sign-Up: 07/07/07

Posts: 1

I get the preloader to work correctly in AS3. When I publish it to the web it works correctly and goes to frame two. However if I hit refresh on the web browser it refreshes and stays on frame one the preloader and does not goto frame two. Any suggestions?


None

studio-nightbird

Reply To Post Reply & Quote

Posted at: 7/7/07 12:43 PM

studio-nightbird NEUTRAL LEVEL 12

Sign-Up: 05/19/06

Posts: 1,059

nice tutorial. i would prob lern that. i just have the newgrounds preloader but just moddifed to the max so its nolonger the newgrounds preloader (as seen in my post 'syndacite the flash seriers)

but great work. you should send this into www.tutorialized.com

www.studionightbird.webs.com(soon moving to a better place)

BBS Signature

None

silverbirch

Reply To Post Reply & Quote

Posted at: 7/7/07 04:29 PM

silverbirch NEUTRAL LEVEL 01

Sign-Up: 07/07/07

Posts: 6

that's awesome!

my only question is this ... doesn't the html page have to load up the entire swf file before you see the preloader anyway? i have a huge flash website that i'm making a preloader for, and i was thinking maybe i should make a separate preloader swf file, and have that swf load and call in the bigger swf. ya know what i'm saying?

i guess i could just put the code into the action for when it's loaded, to load the new swf... but i'm curious why that wasn't mentioned in the tutorial... would love to hear anyone's ideas cause i am no expert :)

thanks!!!


None

joshy24

Reply To Post Reply & Quote

Posted at: 7/7/07 04:37 PM

joshy24 LIGHT LEVEL 06

Sign-Up: 11/28/04

Posts: 714

OK, so will this be going in As Main or anew thread, AS3: Main?

Steam: imuffin101

BBS Signature

None

Paranoia

Reply To Post Reply & Quote

Posted at: 7/7/07 04:39 PM

Paranoia DARK LEVEL 34

Sign-Up: 04/22/05

Posts: 9,697

At 7/7/07 04:29 PM, silverbirch wrote: that's awesome!

my only question is this ... doesn't the html page have to load up the entire swf file before you see the preloader anyway? i have a huge flash website that i'm making a preloader for, and i was thinking maybe i should make a separate preloader swf file, and have that swf load and call in the bigger swf. ya know what i'm saying?

The file is always loaded progressively, so it'll load the frame with the preloader in then that'll show the rest of your movie loading. I think that in some methods of attaching files in IExplore that may not work, but if you use a compatible method of embedding your .swf then you should be A OK.

BBS Signature

None

silverbirch

Reply To Post Reply & Quote

Posted at: 7/7/07 04:53 PM

silverbirch NEUTRAL LEVEL 01

Sign-Up: 07/07/07

Posts: 6

oh well would you look at that.

i just did this according to your isntructions, and it totally worked! lo and behold.

i guess that means that actually flash apps only really download one frame at a time, and then while they're sitting on one frame, they work on downloading the rest.

it's just weird because i saw that my friend had a preloader that was a separate file, and it was preloading and calling in an external swf, and i actually got to see the file with and without the other preloader connected to it, and it did play a lot slower without the preloader. so i don't quite get how that works.

whatever!

:)


None

silverbirch

Reply To Post Reply & Quote

Posted at: 7/8/07 01:37 AM

silverbirch NEUTRAL LEVEL 01

Sign-Up: 07/07/07

Posts: 6

whoa!!!

i just realized something crazy...

this preloader works wonderfully, but if your refresh the page, it simply goes to the preloader frame and doesn't laod at all.... do you know what could be going on with that, or how to fix it? i would really appreciate your help :)

here's my page :

http://www.heartgardens.com/sitemap.html

many thanks!

silverbirch


None

silverbirch

Reply To Post Reply & Quote

Posted at: 7/8/07 02:19 AM

silverbirch NEUTRAL LEVEL 01

Sign-Up: 07/07/07

Posts: 6

oh ok... i've moved the site, just so that other people can still see the working version.

here it is :

http://www.heartgardens.com/sitemap_preloader .html

thanks again!


None

toxic-waster

Reply To Post Reply & Quote

Posted at: 7/8/07 03:39 AM

toxic-waster EVIL LEVEL 09

Sign-Up: 07/14/06

Posts: 185

is action script 3 and easier or better or needed in any way

i am not egocentric......i just self centered
me roar moar money!!!!

BBS Signature

None

Battered-Prawn

Reply To Post Reply & Quote

Posted at: 7/8/07 04:43 AM

Battered-Prawn NEUTRAL LEVEL 12

Sign-Up: 08/02/06

Posts: 1,170

At 7/8/07 03:39 AM, toxic-waster wrote: is action script 3 and easier or better or needed in any way

you can do a helll of a lot more in actionscript. so for coders, its quite a bit leap, but for animators it would probably be easier to stick with the simple AS2

Why not send me a PM, or visit my User Page

BBS Signature

None

silverbirch

Reply To Post Reply & Quote

Posted at: 7/9/07 05:07 PM

silverbirch NEUTRAL LEVEL 01

Sign-Up: 07/07/07

Posts: 6

there are a lot more valuable resources out there at the moment for actionscript 2 users. for that reason alone i'm going to switch over from as3 for a little while... learn as2 and build my own website in it.


None

silverbirch

Reply To Post Reply & Quote

Posted at: 7/9/07 05:10 PM

silverbirch NEUTRAL LEVEL 01

Sign-Up: 07/07/07

Posts: 6

oh by the way

it looks like my server is down... i'm wondering if that might be the reason for the preloader wackiness for that time... things were acting buggy in the file for a little bit right before the server went down.

so i'll let you know if it turns out not to be the server. if it is the server i'll be relieved.


None

WarpZone

Reply To Post Reply & Quote

Posted at: 10/22/07 02:43 PM

WarpZone DARK LEVEL 14

Sign-Up: 01/26/05

Posts: 96

At 5/10/07 06:04 PM, Denvish wrote: You can test your preloader offline, by pressing CTRL+ENTER twice. While in that mode, you can use the View>Download Settings to simulate download speeds ranging from 14.4 kbps modem, up to a T1 line.

Whenever I try this, the whole screen is white, no matter what, until it finishes downloading. The text box does not appear until the Download Simulation is 100% complete. I can't get trace events to fire, either.

This seems consistant with a number of SWFs I used to see with white backgrounds during loading, followed by a 100% loading bar suddenly appearing. You used to see a lot of these right after Flash 9 came out, but not so much anymore.

Is this thread maybe describing how it used to work in an early beta or something, and not how it works now?


None

Armegalo

Reply To Post Reply & Quote

Posted at: 10/28/07 09:58 PM

Armegalo NEUTRAL LEVEL 16

Sign-Up: 07/09/01

Posts: 52

At 10/22/07 02:43 PM, WarpZone wrote:
Whenever I try this, the whole screen is white, no matter what, until it finishes downloading. The text box does not appear until the Download Simulation is 100% complete. I can't get trace events to fire, either.

This seems consistant with a number of SWFs I used to see with white backgrounds during loading, followed by a 100% loading bar suddenly appearing. You used to see a lot of these right after Flash 9 came out, but not so much anymore.

Is this thread maybe describing how it used to work in an early beta or something, and not how it works now?

Is that something to do with when you "export for actionscript" it automatically ticks the box "export in 1st frame". You then need to untick that box on everything that you export.
I'm sure there's an easier method but then I would normally place all the symbols I was exporting in the 1st frame into an object in the 2ndframe and make sure that the user doesn't see it!


None

Armegalo

Reply To Post Reply & Quote

Posted at: 10/28/07 10:15 PM

Armegalo NEUTRAL LEVEL 16

Sign-Up: 07/09/01

Posts: 52

At 7/7/07 12:41 PM, Cluelessdolphin wrote: I get the preloader to work correctly in AS3. When I publish it to the web it works correctly and goes to frame two. However if I hit refresh on the web browser it refreshes and stays on frame one the preloader and does not goto frame two. Any suggestions?

Yup, I get that too. It understand it might be something to do with IE6 - that what you're using?
Anyway, after some fiddling, I came up with something that seems to work... take the above tutorial and replace the code with the following...

stop();
addEventListener(Event.ENTER_FRAME,check Load);
function checkLoad(e:Event):void {
var pcent:Number=this.loaderInfo.bytesLoaded /this.loaderInfo.bytesTotal*100;
lbar.scaleX=pcent/100;
lpc.text=int(pcent)+"%";
if (pcent==100) {
removeEventListener(Event.ENTER_FRAME,ch eckLoad);
this.gotoAndPlay(2);
}
}


Elated

crimsonhalo

Reply To Post Reply & Quote

Posted at: 1/12/08 09:39 AM

crimsonhalo EVIL LEVEL 15

Sign-Up: 05/26/07

Posts: 16

nicce, it was easy to unserstand and u dont miss anythin


None

UnknownFury

Reply To Post Reply & Quote

Posted at: 1/12/08 09:43 AM

UnknownFury EVIL LEVEL 26

Sign-Up: 08/10/05

Posts: 6,031

At 1/12/08 09:39 AM, crimsonhalo wrote: nicce, it was easy to unserstand and u dont miss anythin

What was the point in posting in a 5+ month old topic? Read the rules, pal :)

Bumping any old topic without a good reason and new information.

Portfolio(Under construction): UnknownFury.com |
Msn: giant_ak_47@msn.com | Contact: me@unknownfury.com
Follow me on twitter!


Questioning

BoomBox-Creations

Reply To Post Reply & Quote

Posted at: 4/23/08 03:48 AM

BoomBox-Creations NEUTRAL LEVEL 03

Sign-Up: 04/23/08

Posts: 1

ay if u want, dowload Adobe Flash CS3 Professional from adobe the link:

https://www.adobe.com/cfusion/tdrc/index .cfm?product=flash

and if u like it, and u want the full version give me ur email and ill send u the serail!!

At 5/10/07 08:06 PM, UnknownFear wrote: Nice job, Denvish. Will look into this tutorial and a few of the AS3 tutorials in the AS3: Main when I get my copy of Flash 9.

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

<< Back

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
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!