1

very new to SharedObjects, but essentially all I want to do is let a user answer a question once and not allow them to answer again, how is it possible. This is what I have below?

/*if(_question.data.seenQuestion == "true") {
    cookie_txt.text = "COOKIE FOUND";
} else {
    cookie_txt.text = "NO COOKIES";
}*/


var _question:SharedObject;
_question.data.seenQuestion = "true";
_question.flush();
_question.close();
Peter Bennett
  • 184
  • 1
  • 2
  • 15

2 Answers2

4

You're very close. It looks like you're not actually creating a SharedObject, to do that you would use the method getLocal:

var _question:SharedObject = SharedObject.getLocal("questionData");

Additionally, since SharedObject supports primitive types (String, int, Number, Object, Boolean, etc), you should store a Boolean instead of a String:

if(_question.data.seenQuestion)
{
    cookie_txt.text = "COOKIE FOUND";
}
else
{
    cookie_txt.text = "NO COOKIES";
}

_question.data.seenQuestion = true;
_question.flush();

Lastly, if you're using a local shared object (more common), you don't need to call close().

Marcela
  • 3,728
  • 1
  • 15
  • 21
0

You do need to create your SharedOBject or else your code will throw errors:

var _question:SharedObject = SharedOBject.getLocal("myapp");

No need for flush or close just assign/change some data and that's it.

_question.data.seenQuestion = "true";

However SharedObject is not a good solution for persistent data. User can erase it/reset it, etc ...

BotMaster
  • 2,233
  • 1
  • 13
  • 16
  • You didn't remark the following error: `_question.data.seenQuestion = "true";`. So your answer is uncomplete (voted down). – helloflash Jan 13 '15 at 15:01