Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Saving variable for later use (simple counter app)

New Here ,
Jan 14, 2013 Jan 14, 2013

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.

TOPICS
ActionScript
578
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 14, 2013 Jan 14, 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.t

...
Translate
Community Expert ,
Jan 14, 2013 Jan 14, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 14, 2013 Jan 14, 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 14, 2013 Jan 14, 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;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 15, 2013 Jan 15, 2013
LATEST

Thank you! It works great!

Here's the code I used in case anybody needs it in the future:

var pts:int=0

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

if(so.data.count){

pts=so.data.count;

}

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

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

ptstxt.text = pts.toString();

saveCountF()

}

//this button adds 2 to pts

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines