Game Development Reference
In-Depth Information
you the trouble of having to write three identical event handlers for each bug. They all do exactly the
same thing, so this is just fine.
The only little technical detail you have to be aware of is that you need to use ]oIkrea?hel to tell
AS3.0, “Yes, don't worry, you know what you're doing, this is a Movie Clip object.” The event sends
the p]ncap property as a Opnejc, so you need to cast it as a Ikrea?hel to be able to access the actual
object it refers to.
Making the bugs move
You want the bugs to move like bugs, so some kind of random motion might be a good idea. There
is a formula for random motion that makes objects dither about in no particular direction called
Brownian motion . The formula for Brownian motion looks like this:
\$I]pd*n]j`ki\$%&,*.),*-%
It uses the I]pd*n]j`ki method to generate a random number between -0.1 and 0.1 (Refer to Chapter 5
if you need a refresher on how to generate random numbers.) This number is too small to be much
use for moving an object on the stage, so you need to multiply it by another number to amplify the
effect. Through trial and error, I noticed that multiplying it by 15 looked good for my bugs.
This new formula is then added to the bugs' rt and ru properties:
^qc*rt'9\$I]pd*n]j`ki\$%&,*.),*-%&-17
^qc*ru'9\$I]pd*n]j`ki\$%&,*.),*-%&-17
Add a bit of friction and then add the rt and ru velocities to the bugs' t and u properties.
++=``okiabne_pekj
^qc*rt&9,*517
^qc*ru&9,*517
++Ikrapda^qc
^qc*t'9^qc*rt7
^qc*u'9^qc*ru7
This amazingly mundane code is all that's needed to make the bug object move like a real bug. Makes
you think!
Brownian motion is great if you want to make objects that move in a way that mimic
the organic randomness of insects, dust particles, or snow. Experiment with the friction
value and change the multiplier from -1 to something like 5 or /.

Search Nedrilad ::

Custom Search