Flash - rotating an image around a point

I have been working with Flash recently – when I say Flash I really mean Actionscript – it seems to me the Actionscript and Javascript are getting pretty close together as it was not that difficult to get the gist of the action script.

One of things I was doing involves rotating a dynamically loaded image. This caused me quite a few problems and I had to re-learn all my school trig stuff. Unfortunately this didn’t help very much.

Eventually I found this post that explains how to do it. I have included the code below as well:

this.createEmptyMovieClip("mc1",12);
mc1.createEmptyMovieClip("mc2",14);
mc1._visible = false; // don't want to see the repositioning happen
loadMovie('mypicture.jpg', mc1.mc2);
id = setInterval(positionClip, 400);

function positionClip() {
if(!mc1.mc2._width) // partially loaded yet?
return;
clearInterval(id);
mc1._x = 400;
mc1._y = 300;
// move the 2nd clip relative to the first
mc1.mc2._x = mc1.mc2._x - (mc1.mc2._width / 2);
mc1.mc2._y = mc1.mc2._y - (mc1.mc2._height / 2);
mc1._visible = true;
id = setInterval(spin, 12);
}

spin = function() {
mc1._rotation += 3;
updateAfterEvent();
}

Basically you create an empty ‘holder’ movieclip and then create your image movieclip within that one. Then you position the center of the image over the origin(top left hand corner) on the ‘holder’. The code above should run pretty much as is – obviously you need to put the correct path in for the image. When you publish the movie the image should slowly rotate.

This had all the ingredients to do what I needed but I could not get the width of the image when it was loaded. I was trying check the width of the image with the onLoadComplete for some reason the width kept coming out a zero. The logic seemed right and I was getting a bit annoyed. Then I thought I better do a bit a googling and see if I could find anything about this method. Low and behold I found When Complete is Not Complete which almost instantaneously sorted out the problem by using onLoadInit to trap the width.

Anyway I sorted out the rotation which works perfectly – obviously doing the rotation screwed up another part of the movie but thats another story!

Comments

Leave a Reply