简体   繁体   中英

How to use SharedObject with two Document classes in AS3?

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM