4 Replies Latest reply on May 6, 2013 7:06 PM by lokthelok0

    Place MovieClips at random times in specific locations

    lokthelok0

      I am using Adobe Flash CS6 (primarily on Windows). This is all done in Actionscript 2.

       

      I want to create a grid of:

       

      HAPPYBIRTHDAY

      APPYBIRTHDAYH

      PPYBIRTHDAYHA

      PYBIRTHDAYHAP

      etc. you get the idea.

       

      Each letter is an idividual MovieClip (though I have been using symbols currently not that it makes a difference).

      Each letter needs to appear in the apropriate spot of course to make the words.

      Each letter needs to appear at a random time within say 10 seconds.

       

      I have made a MovieClip for each of the 13 characters (including duplicates, two Ps).

      I have currently made a couple of functions that place the letters from the library with attachMovie().

      I am having some issues just placing all the letters but I can figure that out. What I am having trouble with is how to have all the letters appear randomly.

       

      This shouldn't affect your response but due to the way the letters go together, I have a base  horizontal distance (top right corner to top right corner) of 28 pixels. Then I have variables for each character with values of for example 0.35, 1.7, -0.25. These determine the offset from the initial 28 pixels to make them sit in exactly the right place.

       

      Also I have the AS Linkage names as 0 through 12 for each of the letters. I was using them in a for look in a for loop to vaguely put them out but that didn't do the random timing at all.

       

      This is the code that I wrote which is really a stepping stone to what I want:

       

      var horiz:Number = 28;

      var verti:Number = 28.35;

      var diff0:Number = 0.25;

      var diff1:Number = 1.05;

      var diff2:Number = 1.8;

      var diff3:Number = 0.35;

      var diff4:Number = 0.15;

      var diff5:Number = 0.35;

      var diff6:Number = 0.8;

      var diff7:Number = -0.05;

      var diff8:Number = 0.1;

      var diff9:Number = -1.45;

      var diff10:Number = 0.1;

      var diff11:Number = 1.3;

      var diff12:Number = 0.35;

       

      for(j=0;j<13;j++) {

                for(i=0;i<13;i++) {

                          attachMovie([i+j], "letter1" + i + "_" + j, _root.getNextHighestDepth());

                          _root["letter1" + i + "_" + j]._x = (horiz*i + _root["diff" + i]);

                          _root["letter1" + i + "_" + j]._y = j*verti;

                }

      }

       

       

      for(l=0;l<13;l++) {

                for(k=0;k<13;k++) {

                          attachMovie([k+l], "letter" + k + "_" + l, _root.getNextHighestDepth());

                          _root["letter" + k + "_" + l]._x = (364-horiz*k + _root["diff" + k]);

                          _root["letter" + k + "_" + l]._y = 368.55-l*verti;

                }

      }

        • 1. Re: Place MovieClips at random times in specific locations
          Ned Murphy Adobe Community Professional & MVP

          If you want to use AS2 and have it randomly time something happening, then use the setTimeout function for the timing aspect and the Math.random() method for picking the random time that the setTimelout function uses.

          • 2. Re: Place MovieClips at random times in specific locations
            lokthelok0 Level 1

            I hadn't figured that out but had a fair idea of how to do that. The part that I am struggling with is how to when it chooses to place something, choose randomly one of the letters in the grid then place it in the particular spot it needs to go. I almost need to randomly trigger an instance of the for loop.

            • 3. Re: Place MovieClips at random times in specific locations
              Ned Murphy Adobe Community Professional & MVP

              What you can do is fill arrays with the instances of the movieclips as you create them, one array for each row, essentially a two dimensional array.  Make each movieclip invisible to begin with.

               

              Once they are all planted in the grid formation you can go thru these arrays, randomly picking a set/row and then randomly plucking one of that rows elements out (using the 'splice' method) and turn it visible.

              • 4. Re: Place MovieClips at random times in specific locations
                lokthelok0 Level 1

                I have figured it out and have finished code! I use arrays as you suggested but used them in the process of attaching the movie clips. Then used onEnterFrame to dictate how the letters acted on entry.

                I couldn't get splice to work nicley, so I set the value of the array element to undefined when that letter had been placed.

                 

                You can view it at lharper.tk.

                 

                Here is my code if anyone is interested in the future:

                Each letter is a movieclip in the library with link name 0 through 12.

                var randX:Number;

                var randY:Number;

                var randXY:Number;

                 

                var charDone:Number = 0;

                var timeCount:Number = 0;

                 

                var horiz:Number = 28;

                var verti:Number = 28.35;

                var offsetX:Number = 0;

                var offsetY:Number = 0;

                 

                var diff0:Number = 0.25;

                var diff1:Number = 1.05;

                var diff2:Number = 1.8;

                var diff3:Number = 0.35;

                var diff4:Number = 0.15;

                var diff5:Number = 0.35;

                var diff6:Number = 0.8;

                var diff7:Number = -0.05;

                var diff8:Number = 0.1;

                var diff9:Number = -1.45;

                var diff10:Number = 0.1;

                var diff11:Number = 1.3;

                var diff12:Number = 0.35;

                 

                spot0 = new Array();

                spot1 = new Array();

                spot2 = new Array();

                spot3 = new Array();

                spot4 = new Array();

                spot5 = new Array();

                spot6 = new Array();

                spot7 = new Array();

                spot8 = new Array();

                spot9 = new Array();

                spot10 = new Array();

                spot11 = new Array();

                spot12 = new Array();

                 

                for(i=0;i<13;i++) {

                     for(j=0;j<13;j++) {

                         _root["spot" + i][j] = j

                     }

                }

                 

                var place = function() {

                     rand = random(13);

                     randY = random(13);

                     randX = _root["spot" + randY][rand];

                     if(randX == undefined) {

                         return;

                     }

                     randXY = randX + randY;

                     if(randXY > 12) {

                         randXY -= 13;

                     }

                     attachMovie([randXY], "letter" + randX + "_" + randY, _root.getNextHighestDepth());

                     _root["letter" + randX + "_" + randY]._x = offsetX + (horiz*randX + _root["diff" + randX]);

                     _root["letter" + randX + "_" + randY]._y = offsetY + (randY*verti);

                     _root["letter" + randX + "_" + randY]._alpha = 0;

                     _root["letter" + randX + "_" + randY]._xscale = 50;

                     _root["letter" + randX + "_" + randY]._yscale = 50;

                     _root["spot" + randY][rand] = undefined;

                }

                 

                onEnterFrame = function() {

                     timeCount += 1;

                     trace(timeCount);

                     if(timeCount >= 400) {

                         for(i=0;i<13;i++) {

                               for(j=0;j<13;j++) {

                                   rand = i;

                                   randY = j;

                                   randX = _root["spot" + randY][rand];

                                   if(randX == undefined) {

                                         //nothing

                                   } else {

                                         randXY = randX + randY;

                                         if(randXY > 12) {

                                             randXY -= 13;

                                         }

                                         attachMovie([randXY], "letter" + randX + "_" + randY, _root.getNextHighestDepth());

                                         _root["letter" + randX + "_" + randY]._x = offsetX + (horiz*randX + _root["diff" + randX]);

                                         _root["letter" + randX + "_" + randY]._y = offsetY + (randY*verti);

                                         _root["letter" + randX + "_" + randY]._alpha = 0;

                                         _root["letter" + randX + "_" + randY]._xscale = 50;

                                         _root["letter" + randX + "_" + randY]._yscale = 50;

                                         _root["spot" + randY][rand] = undefined;

                                   }

                               }

                         }

                     } else {

                         place();

                     }

                 

                     for(i=0;i<13;i++) {

                         for(j=0;j<13;j++) {

                               _root["letter" + i + "_" + j].onEnterFrame = function() {

                                   this._alpha += 1.2;

                                   if(this._xscale < 100) {

                                         this._xscale += 0.8;

                                         this._yscale += 0.8;

                                   }

                                   if(this._xscale > 99 && this._xscale < 100) {

                                         //Complete animation action

                                         this.character.textColor = (random(256) * 65536) + (random(256) * 256) + random(256);

                                         charDone += 1;

                                   }

                               }

                         }

                     }

                     if(charDone >= 169) {

                         for(i=0;i<13;i++) {

                               for(j=0;j<13;j++) {

                                   _root["letter" + i + "_" + j].character.textColor += 20;

                               }

                         }

                     }

                }