PDA

View Full Version : Preloader tweaking-Math.round problem



Ubik
February 27th, 2004, 05:50 AM
here's my preloader,
I followed a tutorial from actionscript.org (http://www.actionscript.org/tutorials/advanced/flash_MX_loader/index.shtml) , the tricky part is that i wanted to change the Kb to a % display and it's messing up.

Here's the previous code lastFrame = 1;

function loadedIndicatorFrame() {
var newFrame = int((_parent.getBytesLoaded() / _parent.getBytesTotal()) * 65) + 2;
if (newFrame - lastFrame > 4) { //too far
lastFrame += 4;
loadedText = int(_parent.getBytesTotal() / 1024 * (lastFrame - 2) / 65) + "kb of " + int(_parent.getBytesTotal() / 1024) + "kb";
} else if (newFrame - lastFrame > 0) { //normal move
lastFrame++;
loadedText = int(_parent.getBytesLoaded() / 1024) + "kb of " + int(_parent.getBytesTotal() / 1024) + "kb";
} else { //update the text only
loadedText = int(_parent.getBytesLoaded() / 1024) + "kb of " + int(_parent.getBytesTotal() / 1024) + "kb";
}
return lastFrame;
}


And here's minelastFrame = 1;

function loadedIndicatorFrame() {
var newFrame = Math.round(_parent.getBytesLoaded())/Math.round(_parent.getBytesTotal()) * 52 + 2;
if (newFrame - lastFrame > 4) { //too far
lastFrame += 4;
loadedText = Math.round(_parent.getBytesLoaded())/Math.round(_parent.getBytesTotal())*100 + "%";
} else if (newFrame - lastFrame > 0) { //normal move
lastFrame++;
loadedText = Math.round(_parent.getBytesLoaded())/Math.round(_parent.getBytesTotal())*100 + "%";
} else { //update the text only
loadedText = Math.round(_parent.getBytesLoaded())/Math.round(_parent.getBytesTotal())*100 + "%";
}
return lastFrame;
I post the fla, maybe someone can point me to what's wrong :h:
Thanks
Ubik

scotty
February 27th, 2004, 07:02 AM
Just an idea

var newFrame = Math.round(_parent.getBytesLoaded()/_parent.getBytesTotal()) * 52) + 2;

?

scotty(-:

Ubik
February 27th, 2004, 10:48 AM
Thanks for help,
i changed it (i think i already tried this) but it doesn't work. I have all the decimals showing up, like 33,45863322 and so. Any other idea ?:h:

Ubik
February 29th, 2004, 08:35 AM
Anyone ?:hangover:

scotty
February 29th, 2004, 09:54 AM
lastFrame = 1;
function loadedIndicatorFrame() {
var newFrame = Math.round(_parent.getBytesLoaded()/_parent.getBytesTotal()*65)+2;
if (newFrame-lastFrame>4) {
//too far
lastFrame += 4;
loadedText = Math.round(_parent.getBytesLoaded()/_parent.getBytesTotal()*100)+"%";
} else if (newFrame-lastFrame>0) {
//normal move
lastFrame++;
loadedText = Math.round(_parent.getBytesLoaded()/_parent.getBytesTotal()*100)+"%";
} else {
//update the text only
loadedText = Math.round(_parent.getBytesLoaded()/_parent.getBytesTotal()*100)+"%";
}
return lastFrame;
}

works...
I changed the Math.round thingy (Math.round/Math.round can still give you decimals) and changed the 52 in 65 (your animation is 67 frames long)
You can see it here (http://ut-ovv-9acf.adsl.wanadoo.nl/~gertvink/loader_test.html)

scotty(-:

pom
February 29th, 2004, 10:38 AM
Just as a note, _totalframes will give you the number of frames of your movie.

Ubik
February 29th, 2004, 10:56 AM
Hey, Thanks scotty !
Work, great, i put 52 because i don't the 13 frames after.
In fact i did try to use Math.round as you did but what i did was var newFrame = Math.round((_parent.getBytesLoaded()/_parent.getBytesTotal())*65)+2 like the original code.
I thought it was like mathematics, that if you want to multiply, you had to put it between brackets, but apparently, it's not the case. Well, you helped me great again ! Thanks mate :thumb: !
Cheers

Ubik

Ps: Ilyas, you suggest replacing _getBytesTotal with _totalframes ?

pom
February 29th, 2004, 11:00 AM
Nope, I suggested replacing 65, or 52, or whatever by _totalframes. That way, if you change the nuber of frames of your movie, you don't have to change the code :)

scotty
February 29th, 2004, 11:05 AM
Ubik, no problem:thumb:
Ilyas, thanks for the note and good to see you:)

scotty(-:

Ubik
February 29th, 2004, 11:06 AM
ok !:beam:
Nice indeed, and much more practical,
Thanks for the tip !
Ubik