6 Replies Latest reply on Oct 2, 2007 1:53 PM by Newsgroup_User

    trying to use setInterval command

    splotter
      does anyone have any idea what I am doing wrong, Im trying to load random swfs and have them change on the current screen after a given duration (im still learning and this happens to be in as2)

      filename = ["countdown.swf", "snow.swf", "chimneysweep.swf", "shanghaiedtochina.swf", "ashleydowns.swf"];
      path = " http://testwebsite/flash/swf/";
      i = filename.length;
      k = Math.floor(Math.random()*i);
      loadMovie(path+filename[k], movieTarget);

      var setInterval(loadMovie, 1000);
        • 1. Re: trying to use setInterval command
          Level 7
          splotter,

          > filename = ["countdown.swf", "snow.swf", "chimneysweep.swf",
          > "shanghaiedtochina.swf", "ashleydowns.swf"];

          This line creates a variable named filename and sets it an an instance
          of the Array class. So you have an array with five elements in it. For
          what it's worth, I recommend that you use the var keyword to formally declar
          your variable: Since you're using AS2, you may as well type your variable,
          while you're at it. These extra small steps are good habits to get into,
          because they can be helpful in complex projets.

          var filename:Array = ["countdown.swf", "snow.swf", "chimneysweep.swf",
          "shanghaiedtochina.swf", "ashleydowns.swf"];

          > path = " http://testwebsite/flash/swf/";

          This line creates another variable, this time a string, and sets it to a
          value. Again, I recommend you declare and type your variable:

          var path:String = " http://testwebsite/flash/swf/";

          > i = filename.length;

          Here, you're creating yet another variable, this time a number, based on
          the length of the array. In this context, i equals 5.

          var i:Number = filename.length;

          > k = Math.floor(Math.random()*i);

          Here, you're creating another number variable, whose value is some
          number between 0 and 4. For grins, let's say it happens to be 3.

          var k:Number = Math.floor(Math.random()*i);

          > loadMovie(path+filename[k], movieTarget);

          Here, you're invoking the loadMovie() function with a concatenated first
          parameter. Assuming k is 3, then you're actually saying the following:

          loadMovie(" http://testwebsite/flash/swf/shanghaiedtochina.swf",
          movieTarget);

          ... because path plus the third element from the filename array is the
          string shown.

          > var setInterval(loadMovie, 1000);

          Now you're setting up a timed loop that repeats every second (every
          1,000 milliseconds). The function you're repeatedly triggering is the
          loadMovie() function, but you aren't specifying any parameters this time.

          My hunch is that you want to load a random movie every second, but I'm
          not sure. If I'm right, then you're going to have to re-generate your
          random number every time the function is triggered. I'd wrap the operative
          part in a custom function and trigger that function instead.

          var filename:Array = ["countdown.swf", "snow.swf", "chimneysweep.swf",
          "shanghaiedtochina.swf", "ashleydowns.swf"];
          var path:String = " http://testwebsite/flash/swf/";
          var i:Number = filename.length;

          function loadRandomMovie():Void {
          var k:Number = Math.floor(Math.random()*i);
          loadMovie(path+filename[k], movieTarget);
          }
          setInterval(loadRandomMovie, 1000);

          Now, keep in mind, you don't need the var keyword in front of the
          setInterval() call. setInterval() isn't a variable. The function does
          return a value, so you can store *that*, if you want, but you don't need to
          unless you want to use clearInterval() later to stop the looping. That
          might look like this ...

          var interval:Number = setInterval(loadRandomMovie, 1000);

          ... which means later you could use clearInterval(interval) to stop the
          loop.


          David Stiller
          Co-author, Foundation Flash CS3 for Designers
          http://tinyurl.com/2k29mj
          "Luck is the residue of good design."


          • 2. Re: trying to use setInterval command
            splotter Level 1
            Ok...this makes sense,...I have a blank movie clip that tells flash where my movie clips will be played, well it plays the blank movie clip then starts scrolling through the right movie clips...
            • 3. Re: trying to use setInterval command
              splotter Level 1
              does that make sense?
              • 4. Re: trying to use setInterval command
                Level 7
                splotter,

                >> ... well it plays the blank movie clip then starts scrolling through
                >> the right movie clips...

                > does that make sense?

                I'm afraid not. Try again?


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


                • 5. Re: trying to use setInterval command
                  splotter Level 1
                  This is what I have going on, I created a blank movie clip that tells my swfs where to play on the screen, when the flash file starts playing instead of immediately starting with a random swf it will hold on the beginning movie clip (which is the blank one ) for my given duration then actually play a swf that has content
                  • 6. Re: trying to use setInterval command
                    Level 7
                    splotter,

                    I'm still not sure I follow you, but I'll try.

                    > This is what I have going on, I created a blank movie clip
                    > that tells my swfs where to play on the screen,

                    I'm with you so far. The loadMovie() function accepts a target
                    parameter, and you're routing your external SWFs into an initially empty
                    movie clip whose instance name is movieTarget.

                    > when the flash file starts playing instead of immediately
                    > starting with a random swf ...

                    Nothing is going to happen for a second anyway, because setInterval()
                    doesn't call the loadRandomMovie() until 1000 milliseconds have passed.

                    > ... it will hold on the beginning movie clip (which is the blank
                    > one ) for my given duration then actually play a swf that
                    > has content

                    If you want content loaded immediately, you'll have to call the
                    loadRandomMovie() function immediately. ;)

                    var filename:Array = ["countdown.swf", "snow.swf", "chimneysweep.swf",
                    "shanghaiedtochina.swf", "ashleydowns.swf"];
                    var path:String = " http://testwebsite/flash/swf/";
                    var i:Number = filename.length;

                    function loadRandomMovie():Void {
                    var k:Number = Math.floor(Math.random()*i);
                    loadMovie(path+filename[k], movieTarget);
                    }
                    setInterval(loadRandomMovie, 1000);
                    loadRandomMovie();


                    David Stiller
                    Contributor, How to Cheat in Flash CS3
                    http://tinyurl.com/2cp6na
                    "Luck is the residue of good design."