我在一段时间内第一次在 Actionscript 2.0 中做一些工作(非常简单的东西,只是从文本文件中提取内容),我无法理解为什么我会在这里得到如此不可预测的输出。
有时当我测试构建这样一个简单的脚本时,变量被列为未定义,有时它们不是。
我假设这可能是因为从 cookware.txt 加载的数据尚未加载到内存中,但这似乎并非如此 - 根据此处的 Actionscript 字典,onLoad 函数仅在以下情况下触发数据已被加载,程序的其余部分可以访问。
任何人都可以解释一下吗?或者看看为什么会发生这种情况?
cookware.txt 的内容:
pots=44&kettles=43
代码
_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);
输出::
有时是这样的:
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
有时是这样的:
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
同样,变量是否未定义似乎完全随机。这里发生了什么?