-
problem with loading data from text file into an array????
hi all,
the code is about loading data from text file named:"data.txt"
the data in the file are as following:
&a1=1&
&a2=2&
&a3=3&
&a4=4&
now in the flash file:
in the first frame on stage, code: loadVariables("data.txt", "_root");
then in a button on stage, the code:
on (press) {
_global.arrayData = new Array();
for (var i = 1; i<=4; i++) {
var temp = eval("a"+i);
arrayData[i] = temp;
trace(arrayData[i]);
}
}
in the trace panel, the result is:
undefined
undefined
undefined
undefined
any help with that?
-
.
Hi,
How are you loading the text file?
this might help you along, paste this into a new AS2 document and have the data.txt in the same directory
PHP Code:
var arrayData:Array = new Array();
var loadVars:LoadVars = new LoadVars();
loadVars.load("data.txt");
loadVars.onLoad = function(OK)
{
if (OK)
{
for (var i:Number = 1; i <= 4; i++)
{
var temp:Number = this["a" + i];
arrayData.push(temp);
}
trace(arrayData);
}
};
didnt see your other part, if you want to do it your original way,
loadVariables("data.txt", "_root");
PHP Code:
on (press) {
_global.arrayData = new Array();
for (var i:Number = 1; i <= 4; i++)
{
var temp:Number = _root["a" + i];
arrayData.push(temp);
}
trace(arrayData);
}
and your altered original button code
PHP Code:
on (press) {
_global.arrayData = new Array();
for (var i = 1; i <= 4; i++)
{
var temp = _root["a" + i];
arrayData[i] = temp;
trace(arrayData[i]);
}
}
that lot should get you on
Last edited by fruitbeard; 09-12-2014 at 03:28 PM.
-
thank you fruitbeard, for quick reply!
the first code worked well, however, the 2nt and 3rd with the "press" event gave "undefined" result.
regarding your Q: the data in the file is loading with "loadVariables" function if that what you mean.
-
Originally Posted by fruitbeard
this might help you along, paste this into a new AS2 document and have the data.txt in the same directory
PHP Code:
var arrayData:Array = new Array();
var loadVars:LoadVars = new LoadVars();
loadVars.load("data.txt");
loadVars.onLoad = function(OK)
{
if (OK)
{
for (var i:Number = 1; i <= 4; i++)
{
var temp:Number = this["a" + i];
arrayData.push(temp);
}
trace(arrayData);
}
};
this code was very helpful, thanks again!
-
.
Hi,
Glad you found something that worked for you out of that lot.
Although all three did work here!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|