Basically, I am either having trouble understanding how to use SharedObjects correctly, or I am not properly referencing a variable (from a seperate class from which it is defined) within a SharedObject, and I think the latter is my problem, although I’m not sure.

When my character dies, my game goes to a game over screen, where I create 2 variables within a SharedObject: the bestScore (user’s all-time best score in the game) and then a tutorialBoolean (which will determine if the tutorial shows up or does not in future game play). My bestScore works 100% fine, so I set that up correctly. Now the problem is, in a different class, I want to make an if statement, so that if my tutorialBoolean does not exist or is true, the tutorial runs, and if it is false, the tutorial does not run.

When I try to say

if (sharedObject.data.tutorialBoolean) {runTutorial()}

I get TypeError: Error #1009: Cannot access a property or method of a null object reference. And I am sure that it's having trouble with the sharedObject because I have created a tutorialBoolean without using SharedObjects and it works just fine. So all in all, I just want to be able to make an if statement in separate class than where my sharedObject variable is defined. I will probably have to use

if (sharedObject.data.tutorialBoolean == null)...

or

if (sharedObject.data.tutorialBoolean == "undefined")...

although I'm not really sure which one to use....neither is working at the moment. Shouldn't one of these work no matter what?...I mean if my variable is non existent surely one of these would work...

But I get the error as I said above. I'm not sure if it's worth reconstructing my classes a bit and using sharedObjects in different places, or if there is a simple fix. I like the structure I have at the moment, so the latter is preferable. Thanks!

if (GameOver.sharedObject.data.tutorial1BooleanShared){
// do something
}

I doubt that the sharedObject getter really belongs to the GameOver class, but that's an architectural decision you have to make. Alternatively, you could simply discard the whole getter/accessing via another class and just do the following in your Play-screen:

Some general advice when dealing with SharedObjects: Make sure to wrap your calls dealing with the SharedObject in try/catch blocks (especially flush and maybe even getLocal(). Some people disable writing of SharedObjects alltogether (in the flash player preferences) and your application might crash on their system if you don't catch the error.

Alright awesome! It works! Thanks for the great answer. For some reason I was unable to get the second, more simple looking solution to work, but I didn't try it for very long so maybe I was missing something small. The first solution worked great though and I like it better anyways since it taught me more, cheers =).
–
John DodsonAug 15 '13 at 20:22