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

Can't get SharedObject to work..

Guest
May 17, 2014 May 17, 2014
Basically I've created a score counting program in ActionScript 3, and I want to save the points when I close the program. But I don't understand why the code isn't working..

//The cookie

var store_stats:SharedObject = SharedObject.getLocal("store_stats")

stage.addEventListener(Event.ENTER_FRAME, flusher)

    function flusher (Evt:Event) {

        store_stats.flush();

        store_stats.data.player1stats = player1Score;

        store_stats.data.player2stats = player2Score;

    }

   

trace(store_stats);

//Game stats

var nummer1:Number = 0;

var nummer2:Number = 0;

var player1Score = 0;

var player2Score = 0;

var isDeuce = false;

var isBattle = false

And how to I write on the forum with code font?
TOPICS
ActionScript
418
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
LEGEND ,
May 17, 2014 May 17, 2014

What aspect of the code is not working?

Move that flush() command after you assign the data if the intention is to save that data to the SharedObject.

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
Guest
May 18, 2014 May 18, 2014

When I publish the program and run the swf file, it doesn't save the info till next time I open the program...

Move the flush() command?

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
LEGEND ,
May 18, 2014 May 18, 2014

Yes, the flush() command is what saves the data.  Since you have it placed before the two lines where you assign the data, it is not saving the data you assign... it is saving the previous value if there was any.

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
Guest
May 18, 2014 May 18, 2014

Still doesn't work.

The code looks like this now. When the player1Score (for example) increases and I close the program and restarts it, everything is reset.

//The cookie

var store_stats:SharedObject = SharedObject.getLocal("store_stats") 

                var player1stats = player1Score

                var player2stats = player2Score

                store_stats.data.player1stats;

                store_stats.data.player2stats;

                store_stats.flush();

                              

//Game stats

                var nummer1:Number = 0;

                var nummer2:Number = 0;

                var player1Score = 0;

                var player2Score = 0;

                var isDeuce = false;

                var isBattle = false

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
LEGEND ,
May 18, 2014 May 18, 2014

I cannot tell how that code relates to the code you showed earlier.  For the code you just showed it will execute immediately before any value have a value

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
Guest
May 18, 2014 May 18, 2014
Sorry, of course I meant this:

//The cookie

var store_stats:SharedObject = SharedObject.getLocal("store_stats")

stage.addEventListener(Event.ENTER_FRAME, flusher)

    function flusher (Evt:Event) {

        store_stats.data.player1stats = player1Score;

        store_stats.data.player2stats = player2Score;

        store_stats.flush();

    }

//Game stats

var nummer1:Number = 0;

var nummer2:Number = 0;

var player1Score = 0;

var player2Score = 0;

var isDeuce = false;

var isBattle = false

As the score variables change, they're not being saved in the store_stats. Any idea on what I can do to make it work?
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
LEGEND ,
May 18, 2014 May 18, 2014

It works fine for me.  How are you accessing the scores to know that they are not what you expect them to be?

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
Guest
May 19, 2014 May 19, 2014
Just by referring to the variables from all around the code. I wish I could link the Game Stats variables to the store_stats by simply creating new variables f.ex var player1count = store_stats.data.player1stats; but that won't make any sense, since player1count already exits etc...
Here's the whole thing I'm having an exam in ActionScript in two days, and I really appreciate the help! Thanks.

import flash.events.Event;

stop();

//The cookie

var store_stats:SharedObject = SharedObject.getLocal("store_stats")

stage.addEventListener(Event.ENTER_FRAME, flusher)

    function flusher (Evt:Event) {

      

        store_stats.data.player1rstats = player1score;

        store_stats.data.player2rstats = player2score;

        store_stats.data.player1stats = player1count;

        store_stats.data.player2stats = player2count;

        store_stats.flush();

    }

      

//Game stats

    var player1count = 0;

    var player2count = 0;

    var player1score = 0;

    var player2score = 0;

    var isDeuce = false;

    var isBattle = false

//Shows board when not yet mouseEvent.

    txtPoints1.txtPoints1.text=String(player1count);

    txtPoints2.text=String(player2count);

    player1set.text=String(player1score);

    player2set.text=String(player2score);

  

//PLAYER 1

player1.addEventListener(MouseEvent.CLICK, score1);

function score1(Evt:Event){

    if(isDeuce == true) {

        txtPoints1.txtPoints1.text=("ADVANTAGE");

        txtPoints2.text=("Disadvantage")

        isDeuce = false

        isDeuce == false;

        return; //hopper ut at IFFEN

    }

    if (isBattle == false) {

        player1count += 15;

    }      

    if (player1count == 45) {

        player1count -= 5;

        txtPoints1.txtPoints1.text=String(player1count);

    }

    if (txtPoints1.txtPoints1.text == ("ADVANTAGE")) {

        player1count = 55;

    }  

    if (player1count == 55) {

        trace("Round to Player 1");

        player1score ++;

        player1count = 0;

        player2count = 0;

        player1set.text = player1score

        isBattle=false

    }

    txtPoints1.txtPoints1.text=String(player1count);

    txtPoints2.text=String(player2count);

    player1set.text=String(player1score);

    checkDeuce();

}

//PLAYER 2

player2.addEventListener(MouseEvent.CLICK, score2);

function score2(Evtgsdrsfes:Event) {

    if(isDeuce == true) {

        txtPoints2.text=("ADVANTAGE");

        txtPoints1.txtPoints1.text=("Disadvantage")

        isDeuce = false

        isDeuce == false;

        return; //hopper ut at IFFEN

    }

    if  (isBattle==false) {

        player2count = player2count + 15;

    }

    if (txtPoints2.text == ("ADVANTAGE")) {

        player2count = 55;

    }      

    if (player2count == 45){

        player2count -= 5;

    }

    if (player2count == 55){

        trace("Round to Player 2")

        player2score ++;

        player2count = 0;

        player1count = 0;

        isBattle = false

        txtPoints1.txtPoints1.text=String(player1count);

        player2count = 0;

      

    }

    txtPoints2.text = String(player2count);

    player2set.text = String(player2score);

    checkDeuce();

}

//deuce

function checkDeuce() {

    if (player1count >= 40 && player2count >= 40 && player1count == player2count) {

        txtPoints1.txtPoints1.text = ("DEUCE!");

        txtPoints2.text = ("DEUCE!");

        isDeuce = true

        isBattle = true //Disabling giveaway points(numbers)

        ;

    }

}

(A little unstandardized but it's an old program. One of my first)
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
LEGEND ,
May 19, 2014 May 19, 2014

I see where you assign 0 values to the variables every time the program starts, but I don't see where you assign the SharedObject values to those same variables, which is what I think you are wondering as to why it doesn't happen.

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
Guest
May 19, 2014 May 19, 2014
LATEST

Yes, I think that's the problem. Every time the program starts, they're being set to 0. Therefore overwriting the stored SharedObject values in the same process, leaving the program in a reset state every time I start it. Any workaround for it?

To answer that last one: The SharedObject values are being assigned to the Game Stats variables at the top (isn't that correct?) by:

        store_stats.data.player1rstats = player1score;

        store_stats.data.player2rstats = player2score;

        store_stats.data.player1stats = player1count;

        store_stats.data.player2stats = player2count;

Or must I define the Game Stats variables before the SharedObject values, for them to "connect"? (with that I mean to move the variables above the values, in the script)

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