AS3 Basics: Rotate a movieclip with actionscript 3.0

Just a quick tutorial to go along side my AS2 version of rotation.

Setting up:

Draw a box on the stage, Select it and press ‘F8′.

Name you’re movieclip and press ‘OK’.

Now change the instance name to ‘myMovieClip’ and press enter.

Now for the code:

place this code in frame 1 of you’re actions panel:

// Adding mouse event to our movieclip!
myMovieClip.addEventListener(Event.ENTER_FRAME, rotateMoveClip);

// the rotateMoveClip function
function rotateMoveClip(e:Event)
{
    // e.target is the reference to the MovieClip calling the event
    e.target.rotation += 0.5;
}

Test you’re movie by pressing CTRL+RETURN.

If you’ve managed to follow my bad instructions then you should get a slowly rotating shape.

like this:

Also if you wish to apply the animation to multiple MovieClips simply add the event to those MovieClips like so:

// Adding mouse event to our movieclip!
myMovieClip.addEventListener(Event.ENTER_FRAME, rotateMoveClip);
myMovieClipTwo.addEventListener(Event.ENTER_FRAME, rotateMoveClip);

// the rotateMoveClip function
function rotateMoveClip(e:Event)
{
    // e.target is the reference to the MovieClip calling the event
    e.target.rotation += 0.5;
}

Because we use ‘e.target’ you can add as many movieclips as you like :)

Tags: , ,

Author:Alan Hamlyn

-- Alan Hamlyn Founder of Wuup
  • Luke

    How can I use this more than once?
    (Sorry Im a complete noob to Flash!)

  • http://www.bigtallbill.co.uk Bill Nunney

    Hey Luke, You have to forgive me for this tutorial, its a tad old now (and a bit out of date).

    The article should now be updated accordingly (also a new source FLV is added to the zip) :)

    Happy programming!

  • huzefa Bharmal

    hey,

    how would u speed this up?

    thanks!

  • urs

    Very helpful, simple, and easy. Thanks for explanation!

  • http://www.caiuseugene.com Caius

    Hey, Great Tutorial.

    I’m VERY new to flash and I’m trying my hardest to understand this stuff
    but i can’t :(

    I want to rotate another movie clip (the opposite way) at the same time as when this movie clip rotates, I’ve change the ‘ENTER_FRAME’ for ‘MOUSE_MOVE’. But the thing is I want to movie clip to react to the first movie clip and just rotate opposite.

    Thanks – Caius

  • Bill Nunney

    Hi Caius,
    looks like my source FLA zip link is broken, i’ll try and restore that at some point. To get the movieclip to rotate in the other direction change this line:

    e.target.rotation += 0.5;

    to this:

    e.target.rotation -= 0.5;

  • http://oprotech.com Rohit Manglik
  • naju

    very good tutorial, but i want know one thing, how to duplicate this movie clip with alpha change. i hope youre replay thanking you

  • Bill Nunney

    sorry that site has been hacked by the looks of it

  • Bill Nunney

    Hi Naju, Duplicating MovieClips is actually a bit tricky in AS3.
    this thread http://board.flashkit.com/board/showthread.php?t=739664 has some good suggestions.

    How i would do it is create a new instance of the MovieClip from a library reference and then copy the alpha property’s value to the new one.

    I’m not sure why there isn’t an easier way to copy objects in AS3.

  • Broer Konijng

    Thank you for this tutorial, but I want to rotate this Movieclip at his center. How is this possible?

  • Dan

    Thank you so much, my animation works perfectly! :-D

  • http://osinski.ws V.Osinki

    Hey! Why ENTER_FRAME is “mouse event”??????????

  • Lara

    Hello!
    what should I do if I want to pause the rotation? I want my movie clip to stop rotating when I put the mouse over it.. and then continue from the same position when I roll out the mouse.. help!

  • Guest

    Anybody know how to stop the rotation after one spin?

  • Anonymous

    cool, thanks :)  

  • Niam360

    you want to stop it onclick?