Random Movement
         by Manny Dimatulac aka Thoriphes

Continued...
Like I mentioned a few seconds ago, here is the second page. Let's continue with the code explanation.


(this.vx+this.chgSpd*(Math.random()*2-1))%this.maxV

This is our TRUE condition value. If you recognize part of this from before, you'll see that we add some random value (from -this.chgSpd to this.chgSpd) to the current velocity. The "%this.maxV" keeps it within the maximum velocity limit.

If the velocity reaches above the limit, our modulus (%) brings it back down to zero. If you don't understand how modulus works, refer to the forums for help. There is a lot of kindly folk ready to explain it all to you.


this.vx

This is our FALSE condition value. It simply reassigns the original value if it's not time to add some random value to our velocity.


if (this._x>Stage.width || this._x<0) {
this.vx = -this.vx;
}
 
if (this._y>Stage.height || this._y<0) {
this.vy = -this.vy;
}


This chunk of code simply changes the direction of the object so it "bounces" off the wall. This is the code that's given when you choose "bounce" or nothing (or something else, not "mod") as a behavior.


if (this._x>Stage.width) {
this._x %= Stage.width;
}
 

if (this._x<0) {
this._x = Stage.width-this._x;
}
 

if (this._y>Stage.height) {
this._y %= Stage.height;
}
 

if (this._y<0) {
this._y = Stage.height-this._y;
}

This is the code to make the object appear on the other side of the screen when it goes off. This is called when you choose the "mod" as the behavior.


updateAfterEvent();

I'm not so sure if this line is really needed, but it updates the screen when change takes place, regardless of fps.


this.__INTERVAL = setInterval(this, "$mR", 1000/fps);

This is the code that enables you to still hold on to your onEnterFrame. Basically setInterval repeats some function ($mR) every (1000/fps) milliseconds regardless of the main movie's fps setting. Here I set it so that the animation plays at about the same speed as the Movie's fps. If you want to play the animation at 30 FPS, you set the fps parameter as 30. For 50 FPS, 50, etc.

That pretty much covers the important parts of this tutorial. Now for usage:
someMC.moveRandom(maximum absolute velocity change, maximum velocity, frequency of velocity change, behavior - either "bounce" or "mod" but don't put nothing or it won't work, FPS you want it to play);


Code Usage Examples
The following sections will explain how to implement this effect in your movie:

Dynamically Created Movie Clip - Dot Example

MovieClip.prototype.createDot = function(name, depth) {
        var mc = createEmptyMovieClip(name, depth);
        mc.lineStyle(15, 0, 100);
        mc.lineTo(0.15, 0.45);
        return mc;
};
for (i=0; i<10; ++i) {
        mc = createDot("dot"+i, i);
        mc._x = 275;
        mc._y = 200;
        mc.moveRandom(3, 5, 5, "mod", 30);
}

Paste the above code below the code from the previous page in the same frame.
 


Manually Created Movie Clip
To apply this effect to a movie clip that you manually create, ensure that the object you are interested in moving is indeed a movie clip and add the following code to the movie clip:

onClipEvent(load) {
moveRandom(3, 5, 5, "mod", 30);
}

Note that you can adjust the parameters used in the function to adjust the random movement.


Duplicating an Existing Movie Clip
This is similar to the above example. Ensure that your movie clip has the above code added to it, but also ensure that your movie clip has an instance name. For our example, let's set the instance name of our movie clip to "thoriphes".

Your code to duplicate the movie clip, added to your frame's actions, would simply be:

do {
duplicateMovieClip(thoriphes, "thoriphes"+k, k);
k++;
} while (k != 25);

Where the 25 in the above code refers to the maximum number of movie clips that will be created. Search the site or forums for more information on how to use the duplicateMovieClip function.


I have provided the source file for the example animation found on the previous page (dynamically created dots), and I have also provided an example that uses the random movement code with the duplicated movie clip.

Download Dynamically Created Example (MX2004)
Download Duplicating Movie Clip Example (MX2004)

 
Well, that concludes this tutorial. I hope you enjoy it as much as I enjoyed writing it. As always, questions can be directed to the forum, my e-mail address ([email protected]), or through AIM (boyhatesfrench).

  Manny Dimatulac aka Thoriphes
thoriphes.com

 

Previous Page

 

 

 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.