Simple XML preload problem

Hello guys, long time no see…

function chargeXML() {
    joueurs_xml = new XML();
    loader = new LoadVars();
    joueurs_xml.ignoreWhite = true;
    joueurs_xml.onLoad = function(success) {
        if (success) {
            trace("xml loaded successfully.");
            root = joueurs_xml.firstChild;
            initJoueurs();
        } else {
            trace("xml failed to load.");
        }
    };
    joueurs_xml.load("http://url.xml");    //fake xml address, can't put functionnal addy.
    _root.onEnterFrame = function() {
        preload = joueurs_xml.getBytesLoaded();
        total = joueurs_xml.getBytesTotal();
        percent = (preload/total)*100;
        trace(total);
        trace(percent);
        if (percent>=100) {
            delete _root.onEnterFrame;
        }
    };
}
chargeXML();

I get “undefined” for my var total and thus NaN for percent. What’s going on???

The first time onEnterFrame runs getBytesTotal() will return undefined. I’m not sure. But I think it has something to do with when Flash actually runs a load it has been told to do.

So you would need to add a check if getBytesTotal returns undefined percent is set to 0.


function chargeXML() {
... [cut] ...
        _root.onEnterFrame = function() {
        preload = joueurs_xml.getBytesLoaded();
        total = joueurs_xml.getBytesTotal();
        if(total == undefined) 
        {
                total = 0;
                percent = 0;
        } else
                percent = (preload/total)*100;
        trace(total);
        trace(percent);
        if (percent>=100) {
            delete _root.onEnterFrame;
        }
    };
}

/Mirandir

Thanks Mir, but it doesn’t work…
When I trace the loading, i get :

total 0
percent 0
total 0
percent 0
total 0

percent 0
total 0
percent 0
total 0
percent 0
total 0
percent 0
xml loaded successfully.
total 37153
percent 100

As though it doesn’t see the XML until loaded, and then gives me the right size and percent…

What the hell’s gone wrong??