SharedObjects are Awesome
SharedObject in your flash project can make for a much improved user experience, and can help you do things faster and more behind the sense so that your applications and web sites work the way users expect them to work. Recently I had to discover the power of SharedObject myself in a chat applications that I am building in Flex. Because our application will live on each page of a web site I needed a way to keep the user from having to login to chat everytime they do a page load. The chat app will basically store the users login info locally so that I can easily get their login creds and log them back into the server as they navigate the site. I also went a step farther and store the last few messages that they have either sent or received and load them back into the view when the app loads. In these examples I am going to go over a couple of fundamental things you can do to store user info locally with SharedObjects. Everything in this post was coded using the new Flash Builder Beta but SharedObject has been around for a long time and should work even back in AS2. For my examples we will of course be coding for AS3 and Flash 10. Remembering User Cardinals Here we have an example of storing user login information so when they return to our application we can remember there user name and password for them. Remember that all this data is stored local to the user’s hard drive so we are not transmitting anything over the internet at this point because flash player itself has the data we need. If this is your first time viewing the the app what you need to do is fill in the info and check the box remember me. Then you will refresh the page and you will see that your info was stored. Unchecking the box will clear the data and if you refresh the page you will have to start all over. To show that your data was stored or cleared, click the check SO button. I have some code that will see if the sharedobject data matches what is in the text input controls or not and alert you if the data matches or does not match.
Remembering what the user did last This next example will pretty much do the same thing with sharedobject. Remember all we are really doing is choosing what type of data we need from flash player at runtime. What we are going to do is remember the last thing the user did so they can pickup where they left off. In this simple app we create a ball. The starting X and Y location of the ball is 50px. As you click on the stage the ball will move to the location of the ball to your mouseX and mouseY location. The next time you load the app the ball will move to the last location you clicked. We do this by storing the last mouseX and mouseY location in the sharedobject. Give it a try by clicking around a few times and refreshing the page. If you don’t want the app to remember the location then check the box in the bottom of the app and it will not store the x and y by clearing out the data.
The ball example could let you do something like not skip a flash intro if the user has been to the site before or maybe you only want an animation to play on the home page so after the user goes past that point in the site you store a Boolean value to let you know if they have been there before. Keep in mind that sharedobject data should remain simple. I have gone as far as storing my own value objects in an Array within sharedobjects but if the data grows beyond 100k flash player will warn the user and get their permission before allowing the data to be stored., so you want to keep the data storage small. Hope some of you found this useful and please feel free to leave your comments, suggestions or questions.