Actionscript 3.0: Scale Object from Center Point

Note: This is almost exactly the same solution I posted earlier for rotating an object around its center point (Actionscript 3.0: Rotate Around Center Point). I thought I’d post this slightly altered solution for people who need it.

Problem:

You need to scale a movieclip/sprite from its center point, but its registration point is at (0,0) and you can’t and/or don’t feel like putting this object in the center of a container movieclip and rotating that

Solution:

TCBWMO (Take care of business with the matrix object).

Here’s the function:

private function scaleFromCenter(ob:*, sx:Number, sy:Number, ptScalePoint:Point) { 
	var m:Matrix=ob.transform.matrix;
	m.tx -= ptScalePoint.x;
	m.ty -= ptScalePoint.y;
	m.scale(sx, sy);
	m.tx += ptScalePoint.x;
	m.ty += ptScalePoint.y;
	ob.transform.matrix = m;
}

Now to make this work, you could call it like this:

//this will create a point object at the center of the display object
ptRotationPoint = new Point(mcPhoto.x + mcPhoto.width/2, mcPhoto.y + mcPhoto.height/2);
//call the function and pass in the object to be rotated, the amount to scale X and Y (sx, sy), and the point object we created
scaleFromCenter(mcPhoto, 2, 2, ptScalePoint);

In this example we’re subbing in “2″ for both the sx and sy values. This will scale our image to twice its size. If you want to scale down, use numbers lower than 1.

Comments

One Response to “Actionscript 3.0: Scale Object from Center Point”

  1. seiji on October 21st, 2008 12:30 am

    very helpful. thank you

Leave a Reply