PDA

View Full Version : Textfield and String


inkredibl
01-11-2010, 05:00 PM
Hello everybody

I'm trying to put a text from SharedObject and String to a dynamic text field, but the textfield stays empty (or it's whatever I put in var myName:String = ""; ), and it's ignoring any text I define later for the String object. I'm newbie with this and can't get a clue what's wrong.

Here's the code (Flash lite 2.1, AS 2.0):

var mySO:SharedObject = SharedObject.getLocal("myData");
var myText:String = "";

soInitHandler = function(so:SharedObject){
if(mySO.data.myText == undefined){
myText = "Hello World";

}

else {
myText = mySO.data.myText;
mySO.flush();

}
}
SharedObject.addListener("myData", soInitHandler);

myTextField.text = myText;

After this, myTextField should be "Hello World" or anything, but it's empty. What's wrong with this?

(Ps: It works only if I put myTextField.text whithin soInitHandler function, but it's not such a good solution since I need to reuse String data for other text fields later)

Thanks

crosshair
01-11-2010, 05:33 PM
Welcome to the forum!

Take a look at this:

var myText:String = "Hello World!";

// Save function
function saveData() {
mySO = SharedObject.getLocal("myData");
mySO.data.myText = myText;
mySO.flush();

for (a in mySO.data) {
trace(a+": "+mySO.data[a]);
}
}

// Load function
function loadData() {
mySO = SharedObject.getLocal("myData");

output.text = mySO.data.myText;

for (a in mySO.data) {
trace(a+": "+mySO.data[a]);
}
}

// Delete function
function deleteData() {
mySO = SharedObject.getLocal("myData");
for (a in mySO.data) {
delete mySO.data[a];
}
mySO.flush();
delete mySO;
mySO = SharedObject.getLocal("GAME");
for (a in mySO.data) {
trace(a+": "+mySO.data[a]);
}
}

Thommas
01-12-2010, 09:29 AM
Solution :

var mySO:SharedObject = SharedObject.getLocal("myData");
var myText:String = "";

soInitHandler = function(so:SharedObject){
if(mySO.data.myText == undefined){
myText = "Hello World";

// SOLUTION IS HERE, this should work
myTextField.text = myText;
}

else {
myText = mySO.data.myText;
mySO.flush();

}
}
SharedObject.addListener("myData", soInitHandler);

// Because of asynchronous call to SharedObject, myText here is always ""
// myTextField.text = myText;