1. Home
  2. Computing & Technology
  3. Animation

Dynamic Animation in Flash: Controlling the Drawing API with Equations

By Adrien-Luc Sanders, About.com

6 of 6

The loading handler took care of the starting setup for the dynamic drawing; now we need an onClipEvent (enterFrame) to cause the drawing to create new lines each time the frame cycles:
onClipEvent(enterFrame){
if(xstart>0&&xstart<300&&ystart>0&&ystart<300) {
xstart=xstart*Math.atan(xstart)*Math.random();
ystart=ystart*Math.atan(ystart)*Math.random();
_root.twisty.curveTo(xstart+20*Math.random(), ystart-20*Math.random(), xstart, ystart);
}
else {
ystart=300*Math.random();
xstart=300*Math.random();
}
}

The first thing that the script does is check to make sure that the x and y coordinates in our variables haven’t ranged outside of the visible stage area; only then does it start to do the really random stuff.

What I’ve done is create equations where a new value for each variable is created every time the frame cycles, by taking the previous value and multiplying it by the arctangent of itself (using Math.atan) and then times a number generated by Math.random. I just used arctangent because I liked the way it made the drawing move, but there are a number of Math.function functions that you can use to adjust the behavior, control it, and create more complex equations for more deliberated drawing.

With the new values for xstart and ystart created, the script then uses the drawing API to draw a curve starting at the current (x,y) coordinate and going to coordinates at (xstart,ystart) with curve towards points defined as the original x or y point plus twenty and times a number generated by Math.random. Again, these are just mathematical functions I made up just to see an effect.

There’s also an else statement that resets the values of xstart and ystart should they exceed the stage area.

Explore Animation
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Animation

©2009 About.com, a part of The New York Times Company.

All rights reserved.