• home
  • forum
  • my
  • kt
  • download
  • Home / 2D Graphics / Flash / Backend /

    SharedObject and You in Flash MX

    Author: 2007-06-11 21:45:51 From:

    Hi and welcome to this tutorial. This tutorial covers SharedObject. SharedObject is a kind of cookie for Flash. Though, SharedObject is much more powerful. SharedObject isn't just a way of saving mere strings on the client's hard disk, you can store texts, strings, numbers and even objects. For instance, if you want to save a date object in JavaScript you would have to go though a whole encoding process, with SharedObject you just save it, and be ready.

    Starting with SharedObject

    SharedObject is pretty simple to work with. First you need to call a saved object. This is done with the getLocal() function. getLocal is part of the SharedObject class and expects one parameter: the name of the object you want to call. If the object doesn't exist, it is created; now isn't that nifty. Anyway, the value that is returned by SharedObject.getLocal() is your cookie. It holds all the saved data and the functions you need to contol it. It's really easy to define variables in the sharedObject. All the data is held in the data object. So to define a variable all you have to do is define a variable in the data object. A little example:

    myOjbect = SharedObject.getLocal("test"); //Get the 'test' SharedObject and save it in myObject myObject.data.myName = "Mark"; //Save the value "Mark" in the myName variable in the SharedObject

    Now that the variables are created, there is one more thing you need to know. When you edit the object, the SharedObject isn't saved. If Flash would do that, it would be very inefficient, and would take up a lot of system resources. That's why Flash only saves the SharedObject when you tell it to. You can do that with the flush() function. When you call this function, all the values are saved:

    myOjbect = SharedObject.getLocal("test"); //Get the 'test' SharedObject and save it in myObject myObject.data.myName = "Mark"; //Save the value "Mark" in the myName variable in the SharedObject myObject.flush(); //Save the values

    Now all the values are saved. Calling works in exactly the same way.

    First run
     
    Second run

    Last little tips

    You've now basicly grasped sharedObject. All that remains are a few little leads. First is that you can check if you've allready defined a variable or not by comparing it to undefined:

    if (myOjbect.data.myName == undefined) { //Variable doesn't exist so define it... myObject.data.myName = "Mark"; } else { //Do stuff with the existing variable... trace(myObject.data.myName); }

    Or saving objects to the sharedObject...:

    myObject.data.myArray = new Array(); myObject.data.myObject = new Object(); myObject.data.myObject.myFunction = function () { trace("function called!"); }

    The possibilities are endless :)

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      3D (20)
      Math Physics (14)
      3rd Party (5)
      Navigation (60)
      Actionscripting (26)
      Optimization (16)
      Animation (32)
      Projector (9)
      Audio (46)
      Special Effects (112)
      Backend (25)
      Text Effects (65)
      Drawing (18)
      Tips and Techniques (41)
      Dynamic Content (25)
      Tricks (6)
      Games (66)
      Utilities (19)
      Getting Started (71)
      Video (10)
      Interactivity (21)
      Web Design (22)

    New

    Hot