Enter Frame Event - Rotate Object at Current Frame Rate

Enter Frame Event - Rotate Object at Current Frame Rate

Flash CS4 ActionScript 3.0 Tutorial


How to Rotate Object at a Rate Connected to the Current Flash Movie Frame Rate



The Enter Frame Event happens at your document's current frame rate. In other words, if you have your file's frame rate set to 20 frames per second, the Enter Frame Event would occur 20 times per second. The Enter Frame Event is then connected to a Function. Each time the event fires, the Function is told to run.

In this example the Enter Frame Event happens 20 times per second because the fla file is set to a frame rate of 20. The function tells the bar to rotate 18 degrees every 1/20th of a second. This makes the bar rotate 1 complete rotation (360 degrees) every second.

For more information on Events, see topics, Keyboard Press - Move Object, Mouse Event - Drag Object, Mouse Event - Hide Object and Timer Event - Count a Number.

Tutorial Elements

Movie Symbol with instance name blueBar_mc


Step One

Add a Movie Symbol to the stage.

Step Two

Assign the instance name blueBar_mc to the Movie Symbol.

Step Three

Add a layer above the current content layer and name it Actions.

Step Four

Open the ActionScript panel for your keyframe in the Actions layer by Option double-clicking (PC - Alt double-clicking) the keyframe

Step Five

Copy/Paste the following code in the ActionScript Panel:

addEventListener(Event.ENTER_FRAME, barRotate);

function barRotate(e:Event):void {

blueBar_mc.rotation +=18;


Step Six

Click the Auto Format button to format the code

Step Seven

Test your movie by pressing Cmd/Return (PC - Ctrl/Enter).

Dissecting the Code

The first line of the code says that we are going to run a function 20 times per second because that is the fla file's current frame rate. The name of the function is barRotate.

The next lines of code define the function. Each time the function is called, the bar moves 18 degrees in a clock-wise direction. The +=18 adds 18 more degrees to the bar's current rotation position each time the function is run.