Question

Ok, so I'm working on a quiz game for work. I have each question on its own movie clip (I know that's not the preferred method, but it's what I'm most familiar with and a compressed project timeline kinda forced my hand).

Because players will only be able to play the game once (there's a prize involved), I'm attempting to implement a "save/load" system which will allow players to pickup where they left off, in case their browser crashes, they accidentally close the game window, etc. I already have a SO setup to accept save data. So for example, I have the following code on frame 1 of the Q1 scene:

var currentLevel = "Q1"; //first time the var is defined. "Q1" is the name of the scene
answersSO.data.currentLevel = currentLevel;
answersSO.flush();

And I have the following code on frame 1 of the very first scene of the movie:

function checkProgress() {
if(answersSO.data.currentLevel != undefined) {
    currentLevel = answersSO.data.currentLevel;
    MovieClip(this.root).gotoAndPlay(1, currentLevel);
} else {
    gotoAndPlay(2);
}
}

checkProgress();

First, I gotta say that I'm a real nub at AS. I test play the game up to the Q1 scene, then close the window. I restart the game and it instantly skips all of scene 1 and proceeds to scene 2 (which is an intro to the game, not Q1). No error is thrown.

What I'm looking to do is save some data to the currentLevel variable, and save that variable to the SO. At the start of the game, the script should check if the SO has any data in it. If not, then there is no save data—proceed as normal. If there is data, load it and play the last recorded scene the player was at.

I can't figure out (1) how to get this working and (2), of somewhat less importance, why no error is being thrown.

EDIT!

This is my updated code:

stop();

import flash.net.SharedObject;

var answersSO:SharedObject = SharedObject.getLocal("PWFGame"); //DECLARE SO VARIABLE
var currentProgress = "";

//CHECK SO FOR PROGRESS
checkProgress();

function checkProgress() {
if(answersSO.data.currentLevel == null) {
    nameField.text = "Enter Name Here";
    gotoAndPlay(2);
} else {
    currentProgress = answersSO.data.currentLevel;
    MovieClip(this.root).gotoAndPlay(1, currentProgress);
}
}

It does work, but the last line in the else statement is acting strangely. It skips over the first scene completely and plays the next scene from frame 1 (which is called "Intro"). Even if I change currentProgress to "Q1" it does the same thing. Why would this line of code work elsewhere and not here?

Was it helpful?

Solution 2

Ok, after much digging, I've discovered that gotoAndPlay() on frame 1 can cause issues. I moved the updated code block (seen above) to frame 2, and everything works.

OTHER TIPS

SharedObjects can be deleted by the user. A player could delete that file and start again. If there is a login name, you should store that data on the server using a server side script (php, asp, etc.)

So add a check at the beginning and let the user play just if his name has not been stored on the server. Than call this list form flash at the beginning of the game.

For the browser crash, you can store the data only when the game is ended. So when you refresh the page you can start it again.

If there is no username, you should get other data, but it isn't so simple as an IP could change and more important data can't be retrieved by the flash player (you can do it with a desktop application made with AIR).

At the and... .gotoAndPlay(1, currentLevel);

Remeber that the first value is the scene and the second is the frame.

Nadia

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top