4 Replies Latest reply on Sep 21, 2006 10:44 AM by Newsgroup_User

    Flash Cookie and autoplay

    Level 7
      I am attempting to create a flash piece which will be displayed on the
      home page. I would like to create a button which will allow the user to
      turn off autoplay and store that preference in a cookie.

      I'm not sure the best way to do this. If I load the cookie value, how do
      I tell flash to do something based off that value? This is what I have
      so far, but doesn't seem to be working:

      //Get cookie value
      _root.loadVarObject=new LoadVars();

      lv=_root.loadVarObject;
      lv.randomizer=Math.floor(Math.random()*10000); //Prevent caching
      lv.sendAndLoad("/flash/cookies.asp?method=retrieve", lv, "GET");

      loadVarObject.onLoad = function(success)
      {
      if (success)
      {
      _root.myAutoPlayText.text=this.varAutoPlay;
      }
      }
      //Preloader script
      var totalBytes:Number = getBytesTotal();
      var loadedBytes:Number = getBytesLoaded();
      if (loadedBytes == totalBytes) {
      if (varAutoPlay = "ON") {
      gotoAndPlay(3);
      } else {
      stop;
      }
      } else {
      gotoAndPlay(1);
      }

      Any help is appreciated!
      --


      Seth Meranda
      smeranda2<at>unl<dot>edu
        • 1. Re: Flash Cookie and autoplay
          Level 7
          Seth,

          > I would like to create a button which will allow the user to
          > turn off autoplay and store that preference in a cookie.
          >
          > I'm not sure the best way to do this. If I load the cookie value,
          > how do I tell flash to do something based off that value?

          > //Get cookie value
          > _root.loadVarObject=new LoadVars();
          >
          > lv=_root.loadVarObject;
          > lv.randomizer=Math.floor(Math.random()*10000); //Prevent caching
          > lv.sendAndLoad("/flash/cookies.asp?method=retrieve", lv, "GET");

          At this point, you've initiated a LoadVars.sendAndLoad() call. When
          those GET data are loaded, your lv instance will *have* the reply variables
          as properties.

          > loadVarObject.onLoad = function(success)
          > {
          > if (success)
          > {
          > _root.myAutoPlayText.text=this.varAutoPlay;
          > }
          > }

          Right off the bat, I noticed you've instantiated two objects:
          loadVarObject and lv. You invoked LoadVars.sendAndLoad() on lv, but are
          handling the LoadVars.onLoad event on the *other* instance (that instance,
          loadVarObject, hasn't done anything yet).

          If you handle this event on the lv instance instead, and assuming your
          cookies.asp page returns a name/value pair that sets a value to a variable
          named varAutoPlay, the lv should at this point contain a varAutoPlay
          property with a value.

          Out of curiosity, have you checked out the SharedObject class? It may
          be a much easier mechanism to use for this endeavor.

          http://www.quip.net/blog/2006/flash/actionscript-20/persistent-data-shared-object


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: Flash Cookie and autoplay
            Level 7
            David:

            Thanks for the response. I wasn't aware of the sharedObject, that should
            work great. I'll do a search for some more info.

            I did a quick try based on your code, but something doesn't seem to be
            working correct. Do you see anything wrong with the following AS?

            //Get/set sharedObject value
            var so:SharedObject = SharedObject.getLocal("admisAutoPlay");
            if (so.data.RNAutoPlay) {
            trace(so.data.RNAutoPlay);
            } else {
            so.data.RNAutoPlay = true;
            so.flush();
            }

            //Set the autoplay text value to be displayed
            if (so.data.RNAutoPlay == true) {
            _root.myAutoPlayText.text = "ON";
            } else {
            _root.myAutoPlayText.text = "OFF";
            }
            --


            Seth Meranda
            smeranda2<at>unl<dot>edu
            • 3. Re: Flash Cookie and autoplay
              Level 7
              Seth,

              > I did a quick try based on your code, but something
              > doesn't seem to be working correct. Do you see
              > anything wrong with the following AS?

              > //Get/set sharedObject value
              > var so:SharedObject = SharedObject.getLocal("admisAutoPlay");

              This declares a SharedObject instance by the variable name so. If
              there's already an admisAutoPlay "cookie," its value will be stored in the
              data property of your so variable; if not, this cookie will be created.

              > if (so.data.RNAutoPlay) {
              > trace(so.data.RNAutoPlay);
              > } else {

              If the data property of the so variable contains a property named
              RNAutoPlay whose value is not false, the value will be traced to the Output
              panel.

              > so.data.RNAutoPlay = true;
              > so.flush();
              > }

              Otherwise, a value of true is written to that property and the cookie is
              saved.

              Have you tested this part without the rest of the code? First time you
              test, you won't see anything. Next time you test, you should see "true" in
              the Output panel.


              David Stiller
              Adobe Community Expert
              Dev blog, http://www.quip.net/blog/
              "Luck is the residue of good design."


              • 4. Re: Flash Cookie and autoplay
                Level 7
                Thanks for the help, I have it figured out.

                I put the SharedObject code in frame 1 and moved the rest to frame 2:

                //Get/set sharedObject value
                var so:SharedObject = SharedObject.getLocal("admisAutoPlay");
                if ((so.data.RNAutoPlay != null) && (so.data.RNAutoPlay != true)) {
                _root.myAutoPlayText.text = "off";
                stop(); //Autoplay is turned off, don't display the movie;
                } else {
                so.data.RNAutoPlay = true;
                so.flush();
                _root.myAutoPlayText.text = "on";
                gotoAndPlay(2);
                }

                --


                Seth Meranda
                smeranda2<at>unl<dot>edu