3 Replies Latest reply on Dec 14, 2008 12:24 PM by yogi bear

    set interval problem

    yogi bear Level 1
      I'm trying to generate an instance of a logo every two seconds and have them remain persistent. This code creates a new instance every two seconds but the earlier versions are deleted. Any ideas of how to do this would be helpful.

      function createLogo():Void {
      var counter:Number;
      counter++
      var copy:MovieClip = attachMovie("logo", "newLogo" + counter, counter);
      ["copy" + counter]_x = Math.floor(Math.random() * (0-650));
      ["copy" + counter]_y = -60;
      }




      var myInterval:Number = setInterval(createLogo, 2000);
        • 1. Re: set interval problem
          Rothrock Level 5
          Trace your counter value. I think you will see that it isn't what you think!

          There are probably several things wrong with this. First you can't increment a value that doesn't already have a value. It used to be that a variable without a value was assumed to be zero, but now-a-days...

          The next thing is that you really need to have the counter variable be outside the function. You want it to persist from function call to function call. But by having it declared as var inside the function it is local to the function. So as soon as createLogo is done counter goes away and isn't kept for the next round.

          Finally, this isn't specific to what you have asked, but it is just advice. Generally there can be lots of counters in your code. So it is just fine to name it counter when it is local to a function. But once you move it out of a function (where it might interfere with other counter(s). You might want to rename it to something more identifiable. Like logoCount or some such.
          • 2. set interval problem
            Ned Murphy Adobe Community Professional & MVP
            Place the counter declaration (var counter:Number; ) outside of the function and assign it a starting value as well.

            You are better off using getNextHighestDepth() than to rely on the counter for the depth value... avoids erasing other content if there is any.

            I don't know what you are trying to do with the following 2 lines, but they don't relate to anything in the function...

            ["copy" + counter]_x = Math.floor(Math.random() * (0-650));
            ["copy" + counter]_y = -60;

            Whatever they are talking to will be off screen somehwere.

            Here's a solution for what you are attempting with _x and _y changed for no other reason than to be able to see them...

            var counter:Number;

            function createLogo():Void {
            var copy:MovieClip = attachMovie("logo", "newLogo" + counter, getNextHighestDepth());
            copy._x = Math.floor(Math.random() * (650));
            copy._y = 60;
            counter++
            }

            var myInterval:Number = setInterval(createLogo, 2000);
            • 3. Re: set interval problem
              yogi bear Level 1
              Thanks everyone. Putting the counter outside the function is the answer. I also corrected the code giving the counter a number of 0 and cleaned up the code for the x and y. The reason y was at -60 is that the movie clip loaded moves and starts off screen. Thanks again.