DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 16.5 Sharing Information Between Movies Within the Same Domain

16.5.1 Problem

You want two movies within the same domain to have access to the same local shared object.

16.5.2 Solution

Specify a local path parameter when creating and opening the local shared object.

16.5.3 Discussion

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

D:\Documents and Settings\[UserName]\Application Data\Macromedia\Flash Player\person13.com\ascb\myMovie.swf\myFirstLSO.sol

The name of the .swf file is included in the path to which the LSO is saved so that it will not conflict with an LSO named myFirstLSO 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:

my_l_so = SharedObject.getLocal("myFirstLSO", "/");

If the preceding code exists in myMovie.swf, which is served from www.person13.com/ascb/, the local shared object is stored at the following location:

D:\Documents and Settings\[UserName]\Application Data\Macromedia\Flash Player\person13.com\myFirstLSO.sol

An LSO created in this way can be opened by any other Flash movie in the same domain with the following line of ActionScript:

my_l_so = SharedObject.getLocal("myFirstLSO", "/");

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—www.person13.com—but at different paths. movieOne.swf is served from www.person13.com/ascb/firstGroup/, and movieTwo.swf is served from 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, let's take a look at an example. If the movies don't exist within the same directory, we must specify a local path that is common to both of them in the directory hierarchy. Let's start by looking at what happens if we fail to specify a common local path:

  1. Create a new Flash document, and on the first frame of the main timeline add the following code:

    this.createTextField("message_txt", 1, 100, 100, 100, 20);
    message_l_so = SharedObject.getLocal("messageLSO");
    val = (message_l_so.data.val == undefined) ? 0 : message_l_so.data.val;
    message_l_so.data.val = val;
    message_txt.text = "movie A value: " + val;
  2. Create a new directory somewhere on your computer. Name this directory LSOTest.

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

  4. Save the document as movieA.fla to the LSOTest/movieAPath directory.

  5. Export the movie as movieA.swf to the LSOTest/movieAPath directory.

  6. Create a new Flash document, and on the first frame of the main timeline add the following code (it's almost identical to the code in movieA.fla):

    this.createTextField("message_txt", 1, 100, 100, 100, 20);
    message_l_so = SharedObject.getLocal("messageLSO");
    val = (message_l_so.data.val == undefined) ? 0 : message_l_so.data.val;
    message_l_so.data.val = val;
    message_txt.text = "movie B value: " + val;
  7. Save the document as movieB.fla to the LSOTest/movieBPath directory.

  8. Export the movie as movieB.swf to the LSOTest/movieBPath directory.

  9. Open movieA.swf in a web browser and reload the page several times. Each time you reload, you should see the number increment by one.

  10. Test movieB.swf in the same way you tested movieA.swf. You should see the number increment by one with each reload as well. 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.

  11. To cause both movies to use the same shared object, we 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 in both movieA.fla and movieB.fla:

    message_l_so = SharedObject.getLocal("messageLSO", "/");

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

  12. Save the Flash documents and re-export the .swf files.

  13. Test the movies again as in steps 9 and 10. 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.

16.5.4 See Also

Chapter 17

    [ Team LiB ] Previous Section Next Section