Skip to main content
Participant
January 15, 2013
Answered

Saving variable for later use (simple counter app)

  • January 15, 2013
  • 1 reply
  • 632 views

Hi everyone! First I want to say I'm not very familiar with ac3.

I'm making a simple counter app, basically 1 buttons adds 1, another button adds 2 and another button adds 3. I have this variable (pts) that is the actuall data. What I want to do is to make it save the "pts" data so that its there the next time I open the file.

Here's what I have so far:

var pts:int=0

ptstxt.text = pts.toString();

//ptstxt is the text box that displays the pts value

 

but1.addEventListener(MouseEvent.CLICK, click_handler);

function click_handler(event_object:MouseEvent) {

                    pts += 1

          trace(pts)

ptstxt.text = pts.toString();

}

//this button adds 1 to pts 

but2.addEventListener(MouseEvent.CLICK, click_handler2);

function click_handler2(event_object:MouseEvent) {

          pts += 2

          trace(pts)

ptstxt.text = pts.toString();

}

//this button adds 2 to pts

I searched around and found that it would be for the best if I used the "shared object" but as my knowledge of ac3 isn't big I was unable to do it without help.

Also, would it be usable in an android app?

Looking forwad to a reply and thanks in advance.

This topic has been closed for replies.
Correct answer kglad

:

var pts:int=0

var so:SharedObject=SharedObject.getLocal("counter","/");

if(so.data.count){

// do something with so.data.count which is the value of pts when your app was last used by the client computer.

}

ptstxt.text = pts.toString();

//ptstxt is the text box that displays the pts value

function saveCountF():void{

so.data.count=pts;

so.flush();

}

but1.addEventListener(MouseEvent.CLICK, click_handler);

function click_handler(event_object:MouseEvent) {

                    pts += 1

          trace(pts)

ptstxt.text = pts.toString();

saveCountF();

}

//this button adds 1 to pts 

but2.addEventListener(MouseEvent.CLICK, click_handler2);

function click_handler2(event_object:MouseEvent) {

          pts += 2

          trace(pts)

ptstxt.text = pts.toString();

saveCountF()

}

//this button adds 2 to pts

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
January 15, 2013

:

var pts:int=0

var so:SharedObject=SharedObject.getLocal("counter","/");

if(so.data.count){

// do something with so.data.count which is the value of pts when your app was last used by the client computer.

}

ptstxt.text = pts.toString();

//ptstxt is the text box that displays the pts value

function saveCountF():void{

so.data.count=pts;

so.flush();

}

but1.addEventListener(MouseEvent.CLICK, click_handler);

function click_handler(event_object:MouseEvent) {

                    pts += 1

          trace(pts)

ptstxt.text = pts.toString();

saveCountF();

}

//this button adds 1 to pts 

but2.addEventListener(MouseEvent.CLICK, click_handler2);

function click_handler2(event_object:MouseEvent) {

          pts += 2

          trace(pts)

ptstxt.text = pts.toString();

saveCountF()

}

//this button adds 2 to pts

Nikola_sdAuthor
Participant
January 15, 2013

What exactly did you mean by "// do something with so.data.count which is the value of pts when your app was last used by the client computer.", I didn't quite understand... Should I replace the comment with some action or something?

Your help is appriciated!

kglad
Community Expert
Community Expert
January 15, 2013

yes, presumably you want to do something with that saved value.  possibily you want to re-set pts to that saved value:

if(so.data.count)

pts=so.data.count;

}