I have a multi-dimensional array and I need to save its data into my shared object file and then load it back into the array again. Given that the array has loads of elements I don’t want to do it variable by variable. I saved it out by sending the information out using a for loop and putting in & to spearate the data.
Next I load it up into a variable and convert it to a string. Now I want to read the string and load it up into the array again, incrementing the array every time it hits &. The code is below for this. It is supposed to load up the contents of element [x] [3] :
function loadGame() {
mysavegame = SharedObject.getLocal(“flashcookie”);
if (mysavegame.data.datastring == undefined) {
// No object exists
trace(“No Saved Game”);
} else {
trace(“Loaded Game”);
bigstring = mysavegame.data.datastring;
bigstring.toString();
trace(bigstring); //traces out my data separated by &
k = 0;
for (i=0; i<bigstring.length; i++) {
if (bigstring.charAt(i) != “&”) {
datum += bigstring.charAt(i);
myArray[k][3] = datum;
i++;
} else {
i++;
k++;
trace("myArray [k] [3] is "+(myArray[k][3]));
}
}
}
}
trace(myArray[1][3]);// traces as undefined!!
There’s probably a much more sensible way to do this but I haven’t been doing actionscript long. The trace from this is weird. The first element always seems to get missed and so do the last four. Furthermore when I try to trace the data outside of the load function it traces as undefined. Why?
This is the Trace:
Loaded Game
392000&467200&199800&321600&297600&198400&195000&201600&139200&162000&171600&102600&106000&
myArray [k] [3] is 467200
myArray [k] [3] is 199800
myArray [k] [3] is 321600
myArray [k] [3] is 297600
myArray [k] [3] is 198400
myArray [k] [3] is 195000
myArray [k] [3] is 201600
undefined
Any ideas out there?