Previous Page
Next Page

Recipe 17.7. Sharing Data Between Flash Applications

Problem

You want two movies within the same domain to have access to the same LSO.

Solution

Specify a local path parameter when creating and opening the LSO.

Discussion

By default, LSOs are saved to a path on the client computer that is unique to the domain, path, and name of the .swf file calling the getLocal( ) method. This prevents name conflicts between LSOs from different domains, or even different movies on the same domain. For example, on a system running Windows XP, if a movie named movie.swf served from http://www.person13.com/ascb writes an LSO named example, the data is saved in a location such as the following:

C:\Documents and Settings\[user name]\Application Data\Macromedia\Flash Player\#SharedObjects\[random directory name]\person13.com\ascb\movie.swf\example.sol

The name of the .swf file is included in the path to which the LSO is saved so it won't conflict with an LSO named example created by another movie served from the same domain and path. However, in some cases, you want two movies on the same domain to have access to the same LSO. In these cases, you should use the optional local path parameter when creating and opening the LSO using getLocal( ).

The local path parameter (the second parameter passed to getLocal( )) must be a string that specifies the full or partial path to the .swf file that created the LSO; for example:

var example:SharedObject = SharedObject.getLocal( "example", "/" );

If the preceding code exists in movie.swf, which is served from http://www.person13.com/ascb, the LSO is stored at a location such as the following:

C:\Documents and Settings\[user name]\Application Data\Macromedia\Flash Player\#SharedObjects\[random directory name]\person13.com\example.sol

The difference in this directory versus the one presented earlier is that it lacks the movie and path information. An LSO created this way can be opened by any other Flash movie in the same domain with the following line of ActionScript:

var example:SharedObject = SharedObject.getLocal( "example", "/" );

It is important to understand that a movie can only create and/or open an LSO within the same full or partial path. To understand this, consider an example with two Flash movies: movieOne.swf and movieTwo.swf. Both movies are served from the same domain (http:// www.person13.com) but at different paths. movieOne.swf is served from http:// www.person13.com/ascb/firstGroup, and movieTwo.swf is served from http:// www.person13.com/ascb/secondGroup. In this scenario, movieOne.swf can create and read LSOs with any of the following local path values:

/
/ascb
/ascb/firstGroup

and movieTwo.swf can create and read LSOs with any of the following local path values:

/
/ascb
/ascb/secondGroup

Therefore, if you want both movies to be able to access a common LSO, you must specify one of the two local paths that the movies share (/ or /ascb) when you invoke the getLocal( ) method.

To illustrate how you can share data between two (or more) Flash movies within the same domain, take a look at the following example. If the movies don't exist within the same directory, you must first specify a common local path to both of them in the directory hierarchies. Start by looking at what happens if you fail to specify a common local path:

  1. Create a new .swf movie that contains the following code, and name it movieA.swf:

  2. var count:SharedObject = SharedObject.getLocal( "count" );
    
    // The first time the shared object is read, default the value to 0
    if ( count.data.value == undefined ) {
      count.data.value = 0;
    } else {
      // Every time the shared object is read, increment the count value
      count.data.value++;
    }
    
    // Create a text field to display the count value
    var output:TextField = new TextField(  );
    output.text = "count value: " + count.data.value; 
    addChild( output );
    

  3. Create a new directory somewhere on your computer. Name this directory LSOTest.

  4. Create two subdirectories within LSOTest. Name these subdirectories movieAPath and movieBPath.

  5. Copy the movieA.swf file to both of the subdirectories that were just created.

  6. Rename the movie as movieA.swf to movieB.swf in the LSOTest/movieBPath directory.

  7. Open and close the movieA.swf file multiple times. Each time the file is loaded, the count value should increment by one.

  8. Open and close the movieB.swf file multiple times. Each time the file is loaded, the count value should increment by one. However, notice that the number starts at 0. This is because, as it stands, movieA.swf and movieB.swf use different shared objects. Even though the shared objects have the same name, they have different paths, which created different .sol shared object files.

  9. To cause both movies to use the same shared object, you must tell them to look in the same path. Modify the line of code that opens the shared object so that it reads as follows:

  10. var count:SharedObject = SharedObject.getLocal( "count", "/" );
    

  11. This causes both movies to look to a common path for the shared object, and hence use the same file.

  12. Copy and rename the .swf file with the updated code to the appropriate directories to match previous steps, placing movieA.swf in the movieAPath directory and movieB.swf in the movieBPath directory.

  13. Repeat Steps 5 and 6. This time, notice that each update to one movie also increments the value used by the other movie. This is because they are now using the same shared object.


Previous Page
Next Page
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)