Random Movieclip Placement




  • This tutorial shows how to create movieclips and place them on the stage at random positions.
  • Each clip is given its own level, otherwise it will remove the previous one.
  • When each clip has finished playing it is removed from the stage.
  • Start by creating a new movieclip, I've called mine "randomClipPlacer".
  • Create three layers, one for global variables, one for functions, and 1 for an invisible control movieclip. I put a small black square in mine for visibility.
  • There is one global variable that is initialized to 1 in the globalVariables layer: "clipLevel=1;"
  • You will need to know how far from center you can place a clip.
  • For example your movie is 600 X 400, and the movieclip to be duplicated is 40 X 40.
  • That means the _x range is from -300 to 300 and the _y range is -200 to 200.
  • The movieclip's center is 20 from the edge, so you wouldn't want it placed farther than (280, 180) from the movie's center.
  • So, _xRange = (movieWidth/2)-(clipWidth/2) and _yRange = (movieHeight/2)-(clipHeight/2).
  • Use these figures in the second functions "x=(random(65)*allOver());" and "y=(random(65)*allOver());" code.
  • In the functions layer place the following code for the two functions:


Movieclip.prototype.allOver = function ()
    {
        //This code randomly sets "lrud" (Left, Right,
        //Up, Down) to either 1 or -1
        //This is multiplied by the _x and _y positions
        //in the next function so that the clip can
        //be placed anywhere in the movie
    switcher=random(2);
    if(switcher==1){lrud=1;}
    if(switcher==0){lrud=-1;}
    return lrud;
    }

Movieclip.prototype.addClip = function ()
    {
        //This resets the number used to determine
        //the level the movieclip will be placed
        //Adjust as needed to work with the frequency of
        //generation and length of the movieclips
        //you are using
    if(clipLevel>50){clipLevel=1;}
        //This code creates a new instance of the movieclip,
        //names it and then gives it a level
    attachMovie("clipExport", "mc" + clipLevel, clipLevel);
        //This code sets the coordinates for the clip
    x=(random(65)*allOver());
    y=(random(65)*allOver());
    setProperty("mc" + clipLevel, _x, x);
    setProperty("mc" + clipLevel, _y, y);
    clipLevel++;
    }


  • In the controlClip layer draw a small square then convert it to a movieclip named controlClip.
  • Double click controlClip to edit it.
  • This clip will control how often a new movieClip is placed.
  • Add as many frames to it as needed for the desired interval between generating a new clip.
  • Mine has four frames and the movie is set to 20fps. So every 1/5 of a second a new movieclip is generated.
  • Delete the graphic or add a new layer for the code. Create a keyframe in the last frame and add the code: "addClip();"
  • This calls the addClip function and places a new movieclip on the stage.
  • Right-click the animation that you will be duplicating and select "Linkage".
  • Check the "Export this symbol" box and give it the identifier "clipExport". Now Flash will know where to find it. There is no need to have an instance of the clip on the stage.
  • Last is the destructor that removes the spent movieclips from the stage.
  • In the movieclip you will be duplicating create a code layer with a keyframe in the last frame.
  • Enter the destructor code: "this.removeMovieClip();" to the keyframe. This removes the clip from the stage.


That's it, now you can drag an instance of "randomClipPlacer" from the library and put it to use.

I hope you found this tutorial useful,
Jeff, FlashingTheNet.com






Home :: Resources :: Links :: Contact

:: (c) Copyright 2002 FlashingTheNet.com :: | :: Contact -> Webmaster ::