Forum Topic: AS: Preloaders

(3,892 views • 34 replies)

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

<< < > >>
None

Snubby

Reply To Post Reply & Quote

Posted at: 5/31/06 09:28 PM

Snubby FAB LEVEL 20

Sign-Up: 12/04/04

Posts: 3,145

AS: MAIN by Denvish
AS: Bars by Inglor
AS: Maths - Basic by T-H
AS: Varibales by Rantzien

AS: PRELOADERS
By Snubby

Sorry Denvish, but I feel I can explain how to make a preloader better that you previously did.

-----

What will I learn?
By successfully completing this tutorial you will now how to make dynamic preloaders for both movieclips and the main timeline with...

- Percentage Text
- Kilobytes / Bytes Loaded
- Loading Bars
- Animation Corresponding with Percent

Get a percentage

You will learn about getBytesLoaded() and getBytesTotal()

So to get started I'm going to make a variable called percent. This variable will be responsable for equating the actual percentage of the movie loaded.

getBytesLoaded() - returns the amount of bytes of the movie that are currently loaded.

getBytesTotal() - returns the total amount of bytes in your movie.

Math.round() - rounds a numeric value up or down.

So I'm going to take advantage of these in a simple equation...

percent = Math.round(getBytesLoaded() / getBytesTotal()) * 100;

Now let me walk you through this. To get a percentage on your grade in school, you take your mark, say 22/30, and divide 22 by 30 and then multiply by 100. So when you need to find your percentage when loading a movie, you get the bytes loaded divided by the bytes total, and then multiply by 100. I am round the division because I don't want a value of 55.2987% or 33.3333%. Simple, right?

Making a Percent Loaded Preloader

On the Main Timeline

Create 2 layers, the top named Actions, and the bottom named Percent. The Actions layer should have 2 blank keyframes, and the Percent layer should have 2 frames. On the Percent layer, create a dynamic text box with the var, 'percentText'.

Place this actionscript on the first frame...

percent = Math.round(getBytesLoaded() / getBytesTotal()) * 100;
percentText = percent+"%";

I have consatinated the percent variable with the sting "%", consatinating means to combine a sting and normal varibale. If the percent was equal to 55, this would return, '55%'. So now the percentText has the percent, but we need to constantly update this percent, so on the second blank keyframe, write...

if(_root.percent == 100){
_root.play();
}else{
gotoAndPlay(1);
}

This is a simple if else command. I have used the == to check comparison. In English, this AS states, 'If the percent is not 100%, update the percent again'.

On a Movieclip

Create a movieclip and put it on the first frame of your timeline, nothing else there. Place the following code on your movieclip.

onClipEvent(load){
_root.stop();
}
onClipEvent(enterFrame){
var percent = Math.round(_root.getBytesLoaded() / _root.getBytesTotal()) * 100;
_root.percentText = percent+"%";
if(percent == 100){
_root.play();
}
}

So first off, this script stops the main timeline with the '_root.stop()'. Then we use that percent script on the enterFrame handler, so it will constantly update. Finally, we check if the percent is 100, and if so we play the movie!

Adding the Kilobytes Loaded

1000 bytes = 1 kilobyte. So when we get the bytes loaded or total, like 'getBytesLoaded()', if we were to divide those equations by 1000 we would actually be doing, 'getKilobytesLoaded()', which isn't actually real.

On the Main Timeline

On your Percent layer, add another dynamic text box and give it the var, 'kiloText'. Add this AS on your first keyframe of the Actions layer...

kiloText = (getBytesLoaded() / 1000)+" / "+(getBytesTotal() / 1000);

This is very simple, it just consatinates quotients of the bytes loaded and bytes total, and applies that to the text box.

On a Movieclip

This is simple. Add this line of code at the start of your enterFrame clip handler.

_root.kiloText = (_root.getBytesLoaded() / 1000)+" / "+(_root.getBytesTotal() / 1000);

Presto!

Make a Loading Bar

On the Main Timeline

Make a new layer named 'load bar'. Then, create a bar movieclip, and give it the instance name 'loadBar'. On the Actions layer, add this actionscript...

loadBar._xscale = percent;

_xscale - the PERCENTAGE of a movieclip's width, so in other words out of 100. If a movieclip has a width of 1000 and I say, 'movieclip._xscale = 50;' the movieclip's width will be 500.

This actionscript just sets the _xscale property of the loadBar to the percent, making it mimic the percent.

On a Movieclip

Add this actionscript on your enterFrame clip handler,

_root.loadBar._xscale = percent;

This is the same thing with the _root. prelude infront of the loadBar, for the actual loadBar is on the main (or _root) timeline.

Animation Corresponding with Percent

On the Main Timeline

Make a new layer for your movieclip containing animation, and put it on it. Let this layer have 2 frames. Give your animation movieclip the instance name, 'animation'.

Count the number of frames in your movieclip. Say there are 25. On the first frame of your Actions layer write...

animationFrame = Math.round(percent * (25 / 100));
animation.gotoAndStop(animationFrame);

Replace the bolded number with the number of frames your animation movieclip has. This Actionscript just tells the movieclip to go to the frame of the movieclip that best represents the percent. It rounds the number because all frames in flash are whole numbers (there is no frame 1.1).

On a Movieclip

Add a new layer for your animation movieclip. Move it away from your preloader movieclip, or they will overlap. Add the following AS to the animation movieclip...

onClipEvent(enterFrame){
var animationFrame = Math.round(percent * (25 / 100));
this.gotoAndStop(animationFrame);
}

Like the other script, replace the bolded number with the number of frames your animation movieclip has.

-----

Well, that is my tutorial on Preloaders. Please post comments. I hope you have enjoyed it.


None

Snubby

Reply To Post Reply & Quote

Posted at: 5/31/06 11:31 PM

Snubby FAB LEVEL 20

Sign-Up: 12/04/04

Posts: 3,145

This took me like an hour to write, surely somebody has something to say...

<3 ?


None

Decade-Studios

Reply To Post Reply & Quote

Posted at: 5/31/06 11:34 PM

Decade-Studios NEUTRAL LEVEL 03

Sign-Up: 05/29/06

Posts: 16

Seems pretty well done GJ


None

Snubby

Reply To Post Reply & Quote

Posted at: 5/31/06 11:59 PM

Snubby FAB LEVEL 20

Sign-Up: 12/04/04

Posts: 3,145

At 5/31/06 11:57 PM, bkdude wrote: clockcrew.com has a preloader tutorial. im not gonna get into actioscript at the moment tho. im ok with the NG preloader :-)

hey thats great. i personally hate the NG loader because it is so overused! grrr...


None

fuzz

Reply To Post Reply & Quote

Posted at: 6/1/06 04:49 AM

fuzz NEUTRAL LEVEL 21

Sign-Up: 06/02/04

Posts: 5,654

Good job snubster,
very well explained i think anyone could understand it


None

Paranoia

Reply To Post Reply & Quote

Posted at: 6/1/06 04:59 AM

Paranoia DARK LEVEL 31

Sign-Up: 04/22/05

Posts: 9,119

Yeah, although for the load bar I wouldn't reccomend using the rounded percentage. It's much smoother if you just use the unrounded value. I'd use something like:

decLoaded = _root.getBytesLoaded() / _root.getBytesTotal();
_root.loadBar._xscale = _root.decLoaded * 100;

Your way's alright too though :)

A rate of change in sanity with respect to time.

BBS Signature

None

Snubby

Reply To Post Reply & Quote

Posted at: 6/1/06 03:40 PM

Snubby FAB LEVEL 20

Sign-Up: 12/04/04

Posts: 3,145

At 6/1/06 04:59 AM, _Paranoia_ wrote: Yeah, although for the load bar I wouldn't reccomend using the rounded percentage. It's much smoother if you just use the unrounded value. I'd use something like:

decLoaded = _root.getBytesLoaded() / _root.getBytesTotal();
_root.loadBar._xscale = _root.decLoaded * 100;

Your way's alright too though :)

doh! yup ur tha man. I did it quick and coppied the AS from previous sections of the tutorial as not to write it twice, and forgot to delete the rounding.
:')


None

Simple-Logistics

Reply To Post Reply & Quote

Posted at: 6/1/06 03:57 PM

Simple-Logistics EVIL LEVEL 09

Sign-Up: 05/10/05

Posts: 471

It's good, it's only a bit the same shit Denvish wrote but more detailed.


None

Snubby

Reply To Post Reply & Quote

Posted at: 6/1/06 05:16 PM

Snubby FAB LEVEL 20

Sign-Up: 12/04/04

Posts: 3,145

ya thats what i was aiming for.


None

BritishMoose

Reply To Post Reply & Quote

Posted at: 6/3/06 12:47 PM

BritishMoose NEUTRAL LEVEL 15

Sign-Up: 10/18/03

Posts: 2,933

At 6/1/06 04:49 AM, -fuzz- wrote: Good job snubster,
very well explained i think anyone could understand it

He's right, I did understand.

I don\t get what you mean when you say "animation" for the preloader though. Any chance you could show an example?

Never forget...

BBS Signature

None

Snubby

Reply To Post Reply & Quote

Posted at: 6/8/06 06:34 PM

Snubby FAB LEVEL 20

Sign-Up: 12/04/04

Posts: 3,145

At 6/3/06 12:47 PM, British_Moose wrote:
At 6/1/06 04:49 AM, -fuzz- wrote: Good job snubster,
very well explained i think anyone could understand it
He's right, I did understand.

I don\t get what you mean when you say "animation" for the preloader though. Any chance you could show an example?

http://www.snubbyland.com

on the News page and the FAQ page there is a loader. The animation in that case would be the squares that light up around the title.


Questioning

FogOut9921

Reply To Post Reply & Quote

Posted at: 11/18/06 11:51 AM

FogOut9921 NEUTRAL LEVEL 06

Sign-Up: 09/16/06

Posts: 5

snubs , i still don't understand the 'animation' thing.
Could ya help a man out?


Questioning

FogOut9921

Reply To Post Reply & Quote

Posted at: 11/18/06 11:55 AM

FogOut9921 NEUTRAL LEVEL 06

Sign-Up: 09/16/06

Posts: 5

PS i have seen ur example and wat brit moose said


None

Jamie

Reply To Post Reply & Quote

Posted at: 11/18/06 12:56 PM

Jamie NEUTRAL LEVEL 20

Sign-Up: 11/16/06

Posts: 3,073

Much thank-yous are nessisary :D.


None

Orange

Reply To Post Reply & Quote

Posted at: 1/2/07 09:16 PM

Orange EVIL LEVEL 22

Sign-Up: 07/02/04

Posts: 5,673

Thanks - way better than Denvish's :p

Reason for ban: You have been banned for creating a spam thread, entitled "dick: soft or hard?." Repeated offenses of this nature may result in account deletion.

BBS Signature

Happy

Penboy

Reply To Post Reply & Quote

Posted at: 2/19/07 02:17 AM

Penboy FAB LEVEL 16

Sign-Up: 06/17/06

Posts: 2,872

Wow this is a great NG-Tutorial; so many views, yet not so many replys D:
Nice job :D


None

Memnarch

Reply To Post Reply & Quote

Posted at: 3/4/07 09:10 PM

Memnarch NEUTRAL LEVEL 24

Sign-Up: 04/05/04

Posts: 305

Thanks, I slimed though it and I think I understand most of it for now. I didn't have flash open following along, but I'll try it on my own and see if I mess anything up, and if I do I can just come back here to see what I did wrong.
Once again thanks, I've been looking to learn how to make my own preloader, and yeah the NG preloader is overused.


None

ChilliDog

Reply To Post Reply & Quote

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

ChilliDog NEUTRAL LEVEL 15

Sign-Up: 02/16/07

Posts: 330

This is great, it really helped me out. Thanks Snubby!


None

wsp

Reply To Post Reply & Quote

Posted at: 5/24/07 10:55 PM

wsp FAB LEVEL 03

Sign-Up: 09/10/06

Posts: 206

DAMN YOU i had the EXACT SAME idea just the other day and was gonna make an AS: Main thread on it, comparing to test scores in school, but i was too lazy and didn't want to and u beat me too it.... Very nice explainations and very in-depth btw :D


None

hashbrown

Reply To Post Reply & Quote

Posted at: 5/24/07 10:57 PM

hashbrown LIGHT LEVEL 16

Sign-Up: 07/07/05

Posts: 3,036

now give this to me in 3.0 >:C


None

ekagauranga

Reply To Post Reply & Quote

Posted at: 11/2/07 08:28 PM

ekagauranga LIGHT LEVEL 02

Sign-Up: 08/15/07

Posts: 26

At 5/24/07 10:57 PM, hashbrown wrote: now give this to me in 3.0 >:C

preloader for As3
http://www.bit-101.com/blog/?p=946


Crying

Zeppekk

Reply To Post Reply & Quote

Posted at: 7/26/08 06:37 PM

Zeppekk EVIL LEVEL 13

Sign-Up: 06/28/05

Posts: 485

Anyone care to help me out? I'm trying to make a preloader with a masked bar (It's going to look a bit like the one on 'The last stand' 1 & 2)

http://www.newgrounds.com/bbs/topic/9436 41


None

rickeyricksricky

Reply To Post Reply & Quote

Posted at: 8/14/08 09:19 AM

rickeyricksricky LIGHT LEVEL 02

Sign-Up: 08/12/08

Posts: 66

What's a dynamic text box?

My upcoming games: Slate Circle - A game of strategy
Chuck - A platform game
A torture / interrogation game - No title yet


None

14hourlunchbreak

Reply To Post Reply & Quote

Posted at: 8/14/08 10:44 AM

14hourlunchbreak LIGHT LEVEL 13

Sign-Up: 07/27/07

Posts: 518

I like your tutorial more than Denvish's preloader tut. I actually learned a few things.

Mod's being Gay!
MidnightHowl makes the funniest threads.

BBS Signature

None

rickeyricksricky

Reply To Post Reply & Quote

Posted at: 8/14/08 10:59 AM

rickeyricksricky LIGHT LEVEL 02

Sign-Up: 08/12/08

Posts: 66

At 8/14/08 10:44 AM, 14hourlunchbreak wrote: I like your tutorial more than Denvish's preloader tut. I actually learned a few things.

So what's a dynamic text box? :p

My upcoming games: Slate Circle - A game of strategy
Chuck - A platform game
A torture / interrogation game - No title yet


None

Xivectro

Reply To Post Reply & Quote

Posted at: 8/14/08 11:30 AM

Xivectro NEUTRAL LEVEL 01

Sign-Up: 10/05/06

Posts: 3

[quote] I have consatinated the percent variable with the sting "%", consatinating means to combine a sting and normal varibale. [/quote]

Not trying to be an ass, but did you mean, "Concatenating", the programming term?

Just didn't want to confuse anyone.

PS: The dynamic text box is the feature, in Flash, that does just that, it makes text dynamic.
Meaning your text does not always have to be or say the same thing.
And you update or change what it is within the code using a variable that links to the
dynamis text box.


None

Xivectro

Reply To Post Reply & Quote

Posted at: 8/14/08 11:33 AM

Xivectro NEUTRAL LEVEL 01

Sign-Up: 10/05/06

Posts: 3

Dynamic Text Box *


None

rickeyricksricky

Reply To Post Reply & Quote

Posted at: 8/15/08 12:01 AM

rickeyricksricky LIGHT LEVEL 02

Sign-Up: 08/12/08

Posts: 66

PS: The dynamic text box is the feature, in Flash, that does just that, it makes text dynamic.
Meaning your text does not always have to be or say the same thing.
And you update or change what it is within the code using a variable that links to the
dynamis text box.

How do I create it?

My upcoming games: Slate Circle - A game of strategy
Chuck - A platform game
A torture / interrogation game - No title yet


Elated

VinceDash

Reply To Post Reply & Quote

Posted at: 8/15/08 12:05 AM

VinceDash LIGHT LEVEL 07

Sign-Up: 11/06/07

Posts: 19

you create a normal text box modify its type in the upper left corner of its properties.

Joliette, c'est hot !!!

-Vincent Perreault-


None

rickeyricksricky

Reply To Post Reply & Quote

Posted at: 8/15/08 12:40 AM

rickeyricksricky LIGHT LEVEL 02

Sign-Up: 08/12/08

Posts: 66

Ty guys! :D

My upcoming games: Slate Circle - A game of strategy
Chuck - A platform game
A torture / interrogation game - No title yet


All times are Eastern Standard Time (GMT -5) | Current Time: 07:52 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!