0

I'm having trouble implementing SharedObject in my game.

My Main Document class Engine and a separate class called mcEndGameScreen and in this class which is linked to my Flash CS6 Document.

In my Flash Document there are two Text fields with an instance name of finalScore and bestScore.

What I want to accomplish is saving and loading the final current score that the player got at the end of the game and the overall best score. I want these values to be displayed of course on my mcEndGameScreen.

How I update and keep track of the Highscore which is displayed in the game as the user plays?

HighScore is in my Engine class like so:

//Text Fields
public var highScoreText:TextField;
public var nScore:Number;

In my Engine Function:

//Add Text fields to stage
stage.addChild(highScoreText);

//Add score to text field
nScore = 0; 
updateHighScore();

Then the highscore function:

public function updateHighScore():void
{
   highScoreText.text = "High Score: " + nScore;
}

Now, how would I go about sharing the scores and displaying them on my mcEndGameScreen? Also in my Engine I reference the screen like so:

public var menuEnd:mcEndGameScreen; 

Then I just call the child when the game is over to load up.

But I was thinking of doing something like this in my Engine class maybe:

public var _sharedObject:SharedObject;

Then in my Engine constructor function:

_sharedObject = SharedObject.getLocal("myGame");

But honestly not too sure what to do after that? Or how to use the public var finalScore:TextField and public var bestScore:TextField with my main Engine class. Since they are linked to my mcEndGameScreen.

Any help will be appreciated. Thanks.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Nathan
  • 536
  • 4
  • 21

1 Answers1

1

If you want to pass highScore to mcEndGameScreen class then add new var and new setter function like so:

private var _highScore:String;

public function set highScore(value:String): ():Void {
    _highScore = value;
}

Now in Engine class set it like so:

menuEnd.highScore = String(nScore);

To store highScore in SharedObject do like so:

_sharedObject = SharedObject.getLocal("myGame");
_sharedObject.highScore = nScore;
_sharedObject.flush(); //Write to shared object

To access highScore stored in SharedObject like so:

menuEnd.highScore = _sharedObject.data.highScore;
Rajneesh Gaikwad
  • 1,193
  • 2
  • 14
  • 30
  • Okay so when I add the set highScore function in my mcEndGameScreen class then i can access the highScore and use them with my finalScore and bestScore Text fields in my FLA? How would I go about passing my highScore through the finalScore and bestScore textfields on my mcEndGameScreen class? Just a little confused about that. But thanks for the information. It helped a lot – Nathan Feb 17 '14 at 00:53
  • I get this error when i try to use menuEnd.highScore = nScore; Implicit coercion of a value of type Number to an unrelated type String. – Nathan Feb 17 '14 at 02:12
  • It was type conversion (Number as a String). I have updated the answer. – Rajneesh Gaikwad Feb 17 '14 at 03:57
  • Still a bit confused on how to pass the highscore from my engine class to my mcEndGameScreen classes text field. I have this so far public function bestScoreDisplay():void { bestScoreText.text = ("Best Score: " + ) } But dont know what to add after the + sign – Nathan Feb 17 '14 at 11:53
  • In which class `bestScoreDisplay():` is? – Rajneesh Gaikwad Feb 17 '14 at 11:56
  • `bestScoreDisplay():void { bestScoreText.text = ("Best Score: " + bestScore.text) }` – Rajneesh Gaikwad Feb 17 '14 at 11:59
  • bestScoreDisplay function is in my mcEndGameScreen class. my endgamescreen class has two text fields that are displayed in the FLA. which are the bestScoreText and finalScoreText – Nathan Feb 17 '14 at 12:08
  • Thats why im trying to get the highScore from my Engine class and pass it to the textfields that are in my mcEndGameScreen class. Thanks for all your help by the way. – Nathan Feb 17 '14 at 12:12
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/47661/discussion-between-rajneesh-gaikwad-and-user2233653) – Rajneesh Gaikwad Feb 17 '14 at 12:57