Center pivot point on a sprite
There’s been a long time since I’ve updated this place. Well, real life has gotten me. My time has been divided between work, and Diablo 3 (yeah I know….). These days at work have been very anxious (and still are). There are a tons of things that I want to write, tons of game tutorials I want to add up, but I just keep delaying it. My bad I suppose, but it’s never too late (or so they say).
This time I had an old school problem in as3. I wanted to rotate a sprite, with its pivot point centered. But as you probably know in as3 the pivot point is always at the top left of the sprite, and it can’t be moved except if you are using Adobe CS Flash. Actionscript junkies are left ouf of this. Now there is an algorithm based on mathematics that you can use, but there is an even easier way for this. You probably know it but as I am self-taught I didn’t and spent a lot of time to find it, I guess I could help others who doesn’t know it either.
So to cut to the chase, as you add a DisplayObject to a sprite, you can set its x and y values to minus the half of them.
object.x = -object.width / 2; object.y = -object.height / 2;
As simple as that :). Live example here.