RSS link icon

.

Wheel rotation control with AS3


In this Flash actionscript tutorial we will see how to make an animation rotation, how to control speed with buttons, just a small and fun example.

 

First I just found an image of a rim which I imported into flash, right click and converted to a movie clip.

Remember to give it an instance name, I named mine rim_mc.

flash actionscript rotation

 

Now make two buttons for the wheel speed control, give them both instance names, I named mine, back_btn and forward_btn.

Now go to the actionscript panel and type in the code from below.

// First we set a variable for the speed
var speed:int = 10;

// Then we add an eventlistener to the rim movie clip
// this eventlistener is an enter_frame event (which repeats every time at frame rate).
rim_mc.addEventListener(Event.ENTER_FRAME, wheelRoll);

// this is the function called by the enterframe event
function wheelRoll(event:Event):void
{
    // what we do is to set the event.target.rotation to add a value every time.
    // (The event.taget means the rim_mc).
    event.target.rotation += speed;
}

// two more event listeners these controls the click event for the buttons.
back_btn.addEventListener(MouseEvent.CLICK, wheelBack);
forward_btn.addEventListener(MouseEvent.CLICK, wheelForward);

// the wheel back subtracts a value from the speed.
function wheelBack(Event:MouseEvent):void
{
    speed --;
    trace(speed);
}

// the wheel forward adds a value to the speed rotation variable.
function wheelForward(Event:MouseEvent):void
{
    speed ++;
    trace(speed);
}