Line Rider

Line rider is a very cool flash program that animates a tobogganist sliding along a user created path. There are some amazing paths people have created, just check out YouTube. Anyway, I am currently writing something similar as it is an interesting physics/mathematics challenge.

One problem that came up was rotating objects in Flash. Flash lets you set the angle of rotation, but does not offer a nice way to define the point to rotate around (the pivot point). By default, this is the top left corner of an object. After a fair bit of googling, I found a fancy prototype class that could do it. In the aim of helping others, I thought I’d post my generic function based on the method used in that class. My function is not as fancy, but much easier to use. Note that this does not actually change the pivot point, it just does the rotate based on 0,0 and then moves the object to the correct location to generate the correct rotation position.

The function should be self explainatory. Pass the object(ie. movie clip) as o, px & py are the co-ordinates of the intended pivot point (0,0 would be the top left of the object), r is the angle to rotate by (10 would be ten degrees clockwise).


function rotate(o:Object, px:Number, py:Number, r:Number) {
var a = {x:px, y:py};
o.localToGlobal(a);
o._parent.globalToLocal(a);
o._rotation=o._rotation+r;
var b = {x:px, y:py};
o.localToGlobal (b);
o._parent.globalToLocal (b);
o._x -= b.x - a.x;
o._y -= b.y - a.y;
}

Leave a Reply