View Full Version : Preloader in AS3 (how the hell do i do it)
lambros
June 22nd, 2007, 02:34 AM
hey guys,
i searched almost any topic and didn't found a tutorial about preloaders in AS3.can anyone tell me how to mak a preloader in AS3 ?????? i have th movie clip and the scenes....i just need the actionscript i am gonna put in the preloader's scene so to move to the next scene when the total bytes downloaded are equal the total.
this is the code i use in AS2!
stop();
var yChange = 65;
var yStart = 329;
myInterval = setInterval(preload,100);
function preload() {
var current = _root.getBytesLoaded();
var total = _root.getBytesTotal();
var pctLoaded = Math.round(current/total*100);
mask_mc._y = yStart - yChange/100*pctLoaded;
if (current >= total) {
gotoAndStop("main",1);
clearInterval(myInterval);
}
}
this is for the main movie of my project. also i want to know how to preload external swfs with another preloader movieclip.....please anyone gimme details or a linki to another tutorial......
thanks:goatee: :goatee: :ponder: :ponder: :ponder: :ponder: :ponder:
ecptavares
June 22nd, 2007, 08:12 AM
Try this code posted by starXPloser to answer the same question I had in the past:
stop();
addEventListener(Event.ENTER_FRAME,loadingHandler) ;
function loadingHandler(e:Event){
var bytes_loaded = Math.round(stage.loaderInfo.bytesLoaded);
var bytes_total = Math.round(stage.loaderInfo.bytesTotal);
var getPercent = bytes_loaded/bytes_total;
this.loadBar_mc.width = getPercent*100;
this.loadText.text = Math.round(getPercent*100)+"%";
if (bytes_loaded == bytes_total) {
removeEventListener(Event.ENTER_FRAME,loadingHandl er);
gotoAndPlay(2);
}
}
lambros
June 22nd, 2007, 08:31 AM
well i think this is the right code but how can i make it to be on the first frame of my first scene called "preloader" and then when the movie loads complwetely go to the first frame of my second movie called "main" ??????
i tried to change the gotoandplay(2) to gotoandplay("main") but didn't work
ecptavares
June 22nd, 2007, 08:48 AM
I dont know if this is your case, but I had problens in the past sending it direct to another scene(if main is another scene).What I did was I sent it to another frame(using gotoandplay) and inside this other frame I put gotoAndPlay("scene2");
I havent tried this code because I am busy developing an application but make some tests and let me know how it is flowing.
Also goto ActionScript3.0 forum here in kirupa and check out the tips of the day.There are lots of topics and down on the bottom of the first topic there is a link to a preloading with as3.You might wanna check it out too.
lambros
June 22nd, 2007, 09:49 AM
ok i made it to work as i want but i get some errors when i try to simulate it...
this is the code i am using...
stop();
addEventListener(Event.ENTER_FRAME,loadingHandler) ;
function loadingHandler(e:Event){
var bytes_loaded = Math.round(stage.loaderInfo.bytesLoaded);
var bytes_total = Math.round(stage.loaderInfo.bytesTotal);
var getPercent = Math.round(bytes_loaded/bytes_total*100);
var loadBarw = 260;
loadBar_mc.width= loadBarw/100*getPercent;
loadText_txt.text = getPercent+"%";
if (bytes_loaded >= bytes_total) {
removeEventListener(Event.ENTER_FRAME,loadingHandl er);
gotoAndStop(1, "main");
}
}
it works fine when i test it but when i simulate it it gives me this error and it keeps popping out untill i close the preview...
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at preloaderas3_fla::MainTimeline/loadingHandler()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at preloaderas3_fla::MainTimeline/loadingHandler()
the output window gets filled with this error
what's the problem ?
ecptavares
June 22nd, 2007, 10:17 AM
Thats what I am trying to figure out too..
lambros
June 22nd, 2007, 10:34 AM
oh i am changing some settings now and i found out that the text reaches only till 99% and the bar is not fully loaded.....i think the reason is that the getPercent value doesn't reach 100 exactly till it fills the width and the text. (i think it has something to do with that!!!)
and that depends on the speed i am simulating it on...when i simulate it on dsl it is awsome with 100% and the bar fully loaded....but when i am traying at a faster speed it doesn't get complete but it gets to the next scene.
edit::::
hey guys i found the problem and it's what i mentioned above...when it reaches 99.39%and the bar almost full apart from some pixels it stops there cause according to the formula the action have with the maths.round it cannot fill the whole bar neither the text goes to 100...is there anyone out there that can create a mathimatical sentence that will be accurate ??????
lambros
June 22nd, 2007, 02:32 PM
hey is there anyone outthere that knows AS3 guys ?????????????????? i think many of you solved my problem please tell me what to do to fix that errors and make the text and the bar getting filled compeltely...
if you don't find the solution please then help me with this....i think i found a way to solve this problem but i don't know how to programme it cause i am new to AS3.
look:
let's say we have the "totalbytes" and the " bytesloaded" of the movie...we create the var "bytesperpercentage" which is: (bytesperpercentage = totalbytes/100). And we have another var, the " currentpercentage" which represents the number that will appear in my dynamic text.
i think we must create a statement (do/while or for/next) where we say to flash that starting from 0 to 100, it will download the movie and everytime that the bytesloaded equal the bytesperpercentage it will make the currentpercentage advanced by 1 (currentpercentage++) and display it on screen writing let's say loadtext_txt.text = currentpercentage+"%". then everytime that the "bytesloaded" are equal the "bytesperpercentage" - the currentpercentage it will make the text go one up as it should go....
the problem is that i cannot make it as actionscript correctly. also cause we have the var bytesperpercentage there won't be any problem at the problem we have where the animations don't get fillied completely.
if anyone can create a program like that i think it will be the best solution and the easiest.....not with packages etc... take my code and extend it...i tried something here but it's very uncomplete yet
*/
function loadingHandler(e:Event) {
var bytestotal = stage.loaderInfo.bytesTotal;
var bytesloaded = stage.loaderInfo.bytesLoaded;
var bytesperpct = bytestotal/100;
var currentpct = 0;
var loadBarw = 260;
var bytesperbarpixel = bytestotal/loadBarw;
var currentwidth = 0;
loadText_txt.text = 0 + "%";
for (var i=0; i<=100; i++) {
if (bytesloaded-(currentpct*bytesperpct) == bytesperpct ) {
currentpct = currentpct + 1;
loadText_txt.text = currentpct+"%";
}
}
if (bytestotal == currentpct*bytesperpct ) {
removeEventListener(Event.ENTER_FRAME,loadingHandl er);
gotoAndStop(1, "main");
}
}
i hope that someone of the flash cs3 users understood what i mean with all this and help me....i know for sure that this is possible in visual basic cause i made it last year but i don't remember where i have it and it's been a year since i used vb for the last time so i forgot how to do it. so please help me!!!!!!!
thanks very much!!!
lightscience
June 24th, 2007, 03:29 AM
well... i realize you've changed your code now but i found the type error in your original code!
stop();
addEventListener(Event.ENTER_FRAME,loadingHandler) ;
function loadingHandler(e:Event){
var bytes_loaded = Math.round(stage.loaderInfo.bytesLoaded);
var bytes_total = Math.round(stage.loaderInfo.bytesTotal);
var getPercent = Math.round(bytes_loaded/bytes_total*100);
var loadBarw = 260;
loadBar_mc.width= loadBarw/100*getPercent;
loadText_txt.text = getPercent+"%";
if (bytes_loaded >= bytes_total) {
removeEventListener(Event.ENTER_FRAME,loadingHandl er);
gotoAndStop(1, "main");
}
}
there's no space after ENTER_FRAME, and there should be... also there's an accidental space in the second coloured line there :jailbreak
i am trying to figure out a preloader too... i will give that first code a try there and see if it works for me.
lightscience
June 24th, 2007, 03:39 AM
hi there!!!
ok, so i tried this out, and still even after fixing the spacing and stuff (which might not make a difference after all, i'm not sure), i got the same error as you did.
the reason? i had not added a rectangle called loadBar_mc and a dynamic text field called loadText to my stage yet.
once i did that it worked fine :glasses:
perhaps we're on a completely different wavelength here, but i can see that it's reaching 100% when i use it. if you know how to use code to load up your movie in the right place, why don't you just put that as the function... or add a button that you can click that says "enter" or something, and then have a separate function for that button that takes you there.
i highly recommend for beginners the flash help. it can be frustrating sometimes if you're not sure what to search for, but i've found that i've learned almost everything myself from the help that i needed to know, by learning the best ways to search with it through trial and error. then i had to rely less on my friends in the middle of the night :blush:
good luck!
lambros
June 24th, 2007, 08:25 AM
hey man thanks for th help but this morning i tried to code the preloader just myself from scratch and it works perfectly without errors or anything and it goews to 100% too. thanks for the tips anyway, i am glad that i can get help from this forum!!!
this is the code i use in my current preloader if anyone is interested....
stop();
addEventListener(Event.ENTER_FRAME, loading);
function loading(e:Event) {
var bytestotal = stage.loaderInfo.bytesTotal;
var bytesloaded = stage.loaderInfo.bytesLoaded;
var pct = Math.round(bytesloaded/bytestotal*100);
textloader_mc.loadtext_txt.text = pct;
if (bytesloaded >= bytestotal) {
removeEventListener(Event.ENTER_FRAME, loading);
gotoAndStop(1, "main");
}
}
lightscience
June 24th, 2007, 05:09 PM
yeah sometimes while your'e sleeping your subconscious figures out the code for you : haha.
lambros
June 27th, 2007, 08:44 AM
hey guys i want u to answer a question........
i have placed a loader in my content area and when i click on a button anb external swf is loaded into the source of that loader and that happens for every page......
now for my question: i want to make a preloader for every external swf that it's loaded into my file. i am thinking to make every external swf to have two scenes and in the first scene i will put my preloader just like the main movie !!!!!
is this going to be ok or will it increase the swf size a lot ?????
and if for a reason there is a better way to preload the external swfs just like the code i posted above (which is very simple) please tell me!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.