| |
Building a Random Pointer Field
This tutorial will lead you through the construction of a pointer that
will point at the mouse as you move it around the composition. By the
end, you'll also be able to distribute a group of them across the screen
randomly (read: useless eye candy).
Set up the Composition and Initial Pointer
- Fire up a blank composition, any background color, any size, any framerate.
I used white, 400x400, 30fps.
- Create or import a new object. Make the 'head' of the object point
right. I imported a two color pointer from Illustrator, being sure to
Object:Convert Into:Objects to remove the PDF wrapper.
- Move the anchor point of the object to the 'tail' or the pointer or
the point where you want it to pivot as it rotates. Do this by holding
down the Ctrl (PC) or Cmd (Mac) key, as well as shift (if you want to
constrain movement horizontally) and drag the point to its new position.
- Make the pointer into a Movie Clip with Object:Movie Clip.
Call it 'vector'.
Make the scripted Control Movie Clip and final Pointer Object
Make another object, any settings. This will be the contol clip (that
will tell the pointer what to do), so it will be invisible. I used a little
circle. Object:Movie Clip to make it an MC and rename it 'control'.
Center it over the pivot point of the 'vector' MC. Then make the geometric
object invisible by going into the MC and clicking the eye next to the
object.
Make a movie clip group of the 'control' and 'vector' MCs with Object:Make
Movie Clip Group and name the MCG 'pointer'.
Open up the Script Editor (Ctrl-J or Cmd-J) and go to the 'control'
script area. Be sure you're in the Movie Clip Navigator pane (the 'watch'
icon on the left). Go to the onMouseMove clip event. Type in the following
code:
x = this._xmouse;
y = this._ymouse;;
angle = Math.atan(y/x)/(Math.PI/180);
if (x<0) {
angle += 180;
}
if (x>=0 && y<0) {
angle += 360;
}
this._parent.vector._rotation=angle;
updateAfterEvent();
- The first two lines set up variables with the mouse's position.
- The angle variable determines the angle between the horizontal (0
degrees) and the current mouse position using the inverse tangent function.
Consult with the nearest geometry text (or this)
for help with that. Basically, given the x and y distances from one
object to another, it'll return the angle in radians. The Pi/180 converts
the radians to degrees. Math, sweet math.
- The 'if' clauses make adjustments based on the quadrant the mouse
is in.
- this._parent.vector._rotation=angle; looks at 'this' (the control
MC), down to the parent ('pointer') and then to the other child of 'pointer',
'vector'. It sets the rotation value of 'vector' to the angle of the
mouse we just calculated.
- And then it updates as the mouse moves.
Well, that's it for the pointer itself. Go ahead and test it and stop
here if all you want is a pointer.
Making the Useless Random Pointer Field
- To make the random field, create a script in the first frame of the
composition. In the script editor, go to the Composition level and open
the script area for 00:00:00:00 (has anyone ever made a LIV longer than
an hour in length?!).
- Type or c-and-p the following:
Math.randomBetween = function(a,b) {
return (a + Math.floor(Math.random()*(b-a+1)));
}
count = 1;
while (count<50) {
duplicateMovieClip("_root.pointer","pointer"+count,count);
_root["pointer"+count]._x = Math.randomBetween(10,390);
_root["pointer"+count]._y = Math.randomBetween(10,390);
count += 1;
}
- The Math bit creates a custom function, Math.randomBetween,
which returns a random number between the two given values. Very cool
and useful in general, as Math.random() outputs values between
0.0 and 1.0.
- After initializing the 'count' variable, the while loop duplicates
the entire pointer MCG, calling the new pointers 'pointer(x)', where
x is the current count number. It then puts the new pointer at a random
x,y. Notice that I put a 10 pixel border around the allowable area
(since the comp is 400x400).
- And that should do it. Try it out! Obviously, you can change the
duplication from 50 to however many you want.
|