views:

422

answers:

4

Hi guys,

I'm doing some work in Actionscript 2.0 for the first time in a while (really simple stuff, just pulling content from a text file), and I can not fathom for the life of me why I'm getting such unpredictable output here.

Sometimes when I test build a simple script like this, variables are listed as undefined, and sometimes they aren't.

I'd assume that this might be because the data loaded in from cookware.txt wasn't loaded into the memory yet, but this doesn't seem to be the case - according to the Actionscript dictionary here, the onLoad function only fires when data has been loaded as is accessible to the rest of the program.

Can anyone shed some light? Or see why this could be happening?

Contents of cookware.txt:

pots=44&kettles=43

Code

_global.pots;
_global.kettles;

trace('variables not assigned')
trace('before: kettles (global) = ' + _global.kettles);

trace('before: pots (global) = ' + _global.pots);

var my_lv:LoadVars = new LoadVars();

my_lv.onLoad = function(success:Boolean):Void {
    if (success) {

        trace('variables clearly loaded: kettles = ' + kettles); 

        _global.kettles = this.kettles;

        trace('assigned during loop: kettles in = ' + _global.kettles); 


        trace('pots = ' + kettles);

        _global.pots = this.pots;

        trace('during: pots = ' + _global.pots);

     } else {
        trace("Error");
    }
}


my_lv.load("cookware.txt");

trace('after: kettles (global) = ' + _global.kettles);

trace('after: pots (global) = ' + _global.pots);

Output::

Sometimes it's this:

variables not assigned
before: kettles (global) = undefined
before: pots (global) = undefined
after: kettles (global) = undefined
after: pots (global) = undefined
variables clearly loaded: kettles = undefined
assigned during loop: kettles in = 43
pots = 43
during: pots = 43

and sometimes it's this:

variables not assigned
before: kettles (global) = 43
before: pots (global) = 44
after: kettles (global) = 43
after: pots (global) = 44
variables clearly loaded: kettles = 43
assigned during loop: kettles in = 43
pots = 43
during: pots = 43

Again, whether variables are undefined or not seems totally random. What is going on here?

+1  A: 

You are likely having scope chain issues. The way it works in Flash is very confusing. The short answer is to name your _global.pots and _global.kettles something different than what is being used in your external file to avoid collisions.

The problem being that inside your function, the "kettles" object will refer to the local "this.kettles" object but only if it is defined. If it is not, then it will refer to the "_global.kettles" object. Which makes the setting behavior of OnLoad difficult to predict.

DavGarcia
A: 

Little scope suggestions: create a variable currentTimeline = _level0 (or your levelN), you can use inside the success section of your onLoad function, avoid conflicting names and use _global only if strictly necessary.

A: 

There are possibly some scope issues here, but I just can't see how

before: kettles (global) = 43

ever happens - you haven't even called load yet. And

after: kettles (global) = 43

probably shouldn't work either as the load will execute asynchronously. It would seem to suggest that the flash player is caching _global variables in between plays, but I haven't heard of that happening before. How are you building this? Are you just compiling the swf in the Flash IDE each time?

And yes, what David said about scoping and this, especially in that I would trace this.kettles instead of just kettles for consistency.

Andrew
A: 

Thanks for the help guys - I didn't click before that LoadVars() was an asynchronous function, and that the rest of the script would continue without checking if it was loaded.

I managed to fix this by checking it loadVar has completed on each frame, then executing the code after the loadVar was completed.

I had compiling the swf for testing (command + return).

Chris Adams