Sometimes you want to move an object in a circular path in Flash. Sometimes you don’t. Sometimes you’d rather knit a sweater, or take a nap. But sometimes you do. For those times, I’ve created a walk-through for how to accomplish this in AS3.
A few notes before we begin:
- You might not need this at all. Consider the fact that most circular motion can be simulated by creating an object with an off center axis, and then simply rotating it.
- If you decide that you do indeed need to plot your own circular path, just know that it can get a bit mathy. But fret not, as fretting is not good for your health.
Ok, here we go.
- Get your object ready. Put it on the stage. Give it an instance name. I called mine “red_dot”.
- You’ll need the following data:
- A center point. Your object will rotate around it.
- A radius. Your object will maintain that distance from the center point.
- A step value. How much to move to object in radians per frame around the circle. I call mine angleStep.
var centerX = 150; var centerY = 150; var radius = 80; var angleStep = .01;
Then I set the current angle and cache 2 pi.
var twoPI = 2 * Math.PI; var currentAngle = 0;
Next I need button to start the whole mess. I add an click event listener to the start button.
start_btn.addEventListener(MouseEvent.CLICK, startCircle); function startCircle (e:Event) { stage.addEventListener(Event.ENTER_FRAME, advanceCircle); }
So once the button is clicked, advanceCircle will be called once per frame. Every frame a new angle is calculated in radians by reducing the current angle by angleStep, and a new X and Y are calculated using our old friend the unit circle from trig. Once the current angle goes once around, reset the whole thing, buttons and all.
function advanceCircle(e:Event) { start_btn.removeEventListener(MouseEvent.CLICK, startCircle); currentAngle -= angleStep; red_dot.x = centerX + Math.cos(currentAngle * twoPI) * radius; red_dot.y = centerY + Math.sin(currentAngle * twoPI) * radius; if (currentAngle < -1 ) { currentAngle = 0; stage.removeEventListener(Event.ENTER_FRAME, advanceCircle); start_btn.addEventListener(MouseEvent.CLICK, startCircle); } }
Here is the culmination of this knowledge:


You’re a genious.
Thanks
Hi,
good work,i like to incorporate this in flex3,could you help me?
Thanks in advance..
Sure, sounds like you need yourself a canvas and script. If you can absolutely position your element, this approach should work.
i need something in 3d, i mean rotate in axis-z