PDA

View Full Version : Preloader only works with Actionscript 1.0, not 2.0


2/3Soul
10-23-2003, 05:31 PM
Hey everyone - I have been using previous versions of Flash for quite a while, but never really delved to deeply into the wonders of Actionscript....

My question is regarding some actionscript code that I obtained from one of the tutorials (http://www.actionscript.org/tutorials/beginner/Smooth_PreLoader/index.shtml) on this site a while ago...

Now that I have upgraded to MX 2004, it seems that if I publish the .swf file that contains this code, it doesn't work anymore, but if I change the 'Publish Settings...' to 'Actionscript 1.0', then it works.

The relevant code sits on the preloader movie itself:



onClipEvent (enterFrame) {
loading = _parent.getBytesLoaded();
total = _parent.getBytesTotal();
percent -= (percent-((loading/total)*100))*.25;
per = int(percent);
percentage = per+"%";
loadBar._width = per;
if (percent>99) {
_parent.gotoAndStop(2);
}
}



Now if I am looking at it right, the issue is with the fact that if a variable is previously undefined, Actionscript 2.0 evaluates it in a numerical context as "NaN" instead of "0", which screws everything up for the 'percent' variable.

Has anyone got any ideas for trying to fix this ? :confused:


Cheers!

hokoga_saeos
10-23-2003, 06:04 PM
im not to sure but i have had the same problem as you with that preloader ,,and a few days ago i was asking about it here in these forums ..
but i thank you for the info ..i am going to go and check it out now and see if it works again ..i love that preloader .in mx6 it worked just fine but a few days ago i need it and it didnt work in mx 2004 pro ..,,so again thx..

ferdymania
10-23-2003, 09:22 PM
i think percent is undefined because it hasnt been initialised..
try initialising all the variables in the onload..

_root.onLoad = function(){

var percent:Number = 0;
var loading:Number = 0;
//etc

}


in AS1.0 variables are pre-initialised to 0 , etc..
in AS2.0 they are not initialised , but undefined...

everyone is getting trapped by this..
steve

Colin Campbell
10-23-2003, 09:27 PM
Yep, ferdy's right, you need to declare variables in AS2:


onClipEvent (enterFrame) {
var loading = _parent.getBytesLoaded();
var total = _parent.getBytesTotal();
var percent -= (percent-((loading/total)*100))*.25;
var per = int(percent);
var percentage = per+"%";
loadBar._width = per;
if (percent>99) {
_parent.gotoAndStop(2);
}
}

Try to read up on AS 2, alot of aspects are different. Macromedia has really matured actionscript into a tightly controlled language. By that I mean its really structured now.

2/3Soul
10-24-2003, 08:51 AM
Thanks for the help guys... I managed to sort it out myself anyway sometime last night (from what you've all written, you came up with similar solutions). For info, this is what I changed the code to....



onClipEvent (load) {
percent = 0;
}
onClipEvent (enterFrame) {
loading = _parent.getBytesLoaded();
total = _parent.getBytesTotal();
percent -= (percent-((loading/total)*100))*.25;
per = Math.round(percent);
percentage = per+"%";
loadBar._width = per;
if (per>99) {
_parent.gotoAndStop(2);
}
}



It seems without predefining 'percent' as a variable it wouldn't work, so I used the 'onClipEvent (load)' function to define percent, for the first time the movieclip was initialised.

Also, the 'int()' function has been deprecated, and stops this bit of code working when it hits 99%, and the preloader never finishes, and just stalls. Using the 'Math.round()' function solves this problem.

Cheers...