0

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?

mr_saturn
  • 3
  • 2
  • First, do trace what's in `answersSO.data.currentLevel`, maybe there's garbage. Maybe you didn't initialize the SO properly, so it's overwritten each time you start the application. – Vesper Jan 24 '14 at 04:45
  • Thanks I just tried that. It's saving Q1 into the SO. I tried changing the data to '"Q1"' so that the SO stores the quotes as well. Well, now an error is thrown that scene "Q1" is not found. – mr_saturn Jan 24 '14 at 14:03

2 Answers2

0

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

Nadia
  • 247
  • 1
  • 13
  • I'm not concerned with users deleting SOs. I'm aware of their limitations, but I work in a corporate office and the overwhelming majority of people will not have any idea how to do that, even if they knew what a SO was. – mr_saturn Jan 24 '14 at 13:52
  • there are software like ccleaner doing it in one click. Anyway it depends on the target of the user, but if there are prizes it's easy that someone will try to cheat. – Nadia Jan 24 '14 at 14:09
  • anyway, did you try: ...gotoAndPlay(currentLevel , 1); – Nadia Jan 24 '14 at 14:20
  • The syntax I have in the rest of the movie is gotoAndPlay(frame#, "scenename") which works. The other way around doesn't. It throws an error which basically says I'm trying to use a string where I should have a number. Error 1067 – mr_saturn Jan 24 '14 at 14:23
  • Currently, it appears to be loading the SO data, but now it's telling me the scene can't be found. And the condition of the IF statement simply will not work. I can't figure out another way that will, so far. – mr_saturn Jan 24 '14 at 14:25
  • if we don't see the full code is not easy to check... anyway is it possible that on other parts of the script it was pointing to a frame label and not to a scene? as you can see: http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000566.html If you have one var, it indicates the frame (number or label), while with two vars, the first is the scene and the second in the frame). Probably in your script there is no "Q1" label and you get this error. – Nadia Jan 24 '14 at 16:56
  • I have the following code in other parts of the game and it works: MovieClip(this.root).gotoAndPlay(1, "Q1"); However, after much testing, I've discovered that this is the code giving me issues in this frame alone (scene 1, frame 1). I have no idea why. – mr_saturn Jan 24 '14 at 18:01
0

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.

mr_saturn
  • 3
  • 2