Johnny G Junior's NoteBook Treasures: % Loaders: understanding lesson

Monday, April 28, 2008

% Loaders: understanding lesson

Math is very amazing... and the thing is we do it every day. Lets take the good old %. YOu go to a restaurant and its time to leave. you get your bill and its $25. How do you determine the 15% tip. FLASH BACK.. basic math.

EQUATION...

15% X
--- = ----
100% $25

Percentage Part
--------- = ---------
100% Total


we figure it out by cross multiplying. (15 x 25) / 100 = X
the answer is $3.75.
This is NO DIFFERENT THEN FIGURING OUT a percentage for flash. you just need to know the variables.

PERCENTAGE Bytes Loaded
------------ = -------------
100 Bytes Total


getBytesLoaded() - returns a numeral that have been downloaded of CURRENT MOVIE
getBytesTotal() - returns a numeral that of total bytes
_root - targets Main Timeline
_parent - targets Timeline ABOVE current movieclip timeline or object
_this - targets CURRENT timeline
1024 bytes are in a 1 Kbyte

Math equation example.

kbytesloaded = _root.getBytesLoaded()/1024;

kbytes is a variable and it ='s _root (main timeline) getBytesLoaded() (return number of loaded byte) divide by 1024. kbytesloaded will now = a number of bytes loaded in Kbytes.


Ok. anyyyway.

The equation for a percentage looks like this.

percentage = (Math.floor(_root.getBytesLoaded()/_root.getBytesTotal()*100));


percentage (variable) = Math.floor(round integer) of (bytesloaded/bytestotal * 100) same as finding a percentage of a TIP you leave at a restaurant. (look above to reference).

ok great. We now have a Number in the percentage variable, but thats all you will get. 1 number. you have to check that action script over and over again to have a new number... how do we do that. Well we make a loop. once it passes that frame in flash, in the next frame, just have it go back to the previous frame

gotoAndPlay(2); (if the script is on frame 2 - which it should be)

now it will constantly be in that loop.. no matter what... UNLESS you make some kind of CONDITION... if i go back to frame 2 and the bytesloaded and bytetotal are equal to eachother... then lets move to a total different frame (other then the next one because it will loop us again).

if (_root.getBytesLoaded() == _root.getBytesTotal()){
gotoAndPlay(3);
}


== means THE SAME AS. this script will go UNDER the loading check script.


how do i make a bar grow.. well the idea is that you scale the bar according to how much of a percentage is loaded...

make a MovieClip that has a colored box in it. heres the script.

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


look familar. this._xscale = effect this timeline's xscale. the rest is the same equation as before. We didnt use Math.floor because we dont want it to only scale by WHOLE NUMBERS.. part numbers are fine.

I will post a complete script of a loader for flash... but this will help you understand it completely.

be good.

No comments: