Monday, May 18, 2009

Registration Point Details(AS 3)

Registration Point is the (0,0) point of any display object . All the scaling , rotation etc take place with respect to this point . So its very much important to get a clear idea about registration point for any Action Script Developer. Sometimes when working with Action Script i faced several problems and recover them . Here i have tried to share my findings.

As i said earlier Registration point is the (0,0) point of any display object , so how to set it ? If the display object is not dynamically created then its a matter of dragging and dropping only. But if the display object is dynamically created then its a matter of calculation and planning.

Say you have to make a display object that is actually a rectangle that scales with respect to its center and you need it all dynamically . Here two points to be considered one Registration point is the point at (0,0) so to scale the object with respect to the center the rectangle should be drawn such way so the (0,0) point becomes the center of the rectangle so the display object scales or rotates around it .

But for complex display object or due to any other reason the Registration point may needed to be adjusted in many operations with many values . To do this only four steps are enough:
  1. get the value of the changed registration point with respect to the object where the display object under consideration is added .
  2. run the operation , rotate , scale
  3. get the value of the changed registration point with respect to the object where the display object under consideration is added after the operatoin .
  4. calculate the difference of the two points and minimize them in the x and y value of the display object
so a function like,

function doOperationRegChanged(op:String, val:Number,xreg:Number, yreg:Number):void {
var nrp:Point=new Point(xreg, yreg);
var bop:Point = this.parent.globalToLocal(this.localToGlobal(nrp));
this[op] = val;
var :Point = this.parent.globalToLocal(this.localToGlobal(nrp));
this.x -= b.x - a.x;
this.y -= b.y - a.y;
}
under a display object for which different registration point needed for different operation can be handled by calling
mc.doOperationRegChanged('scaleX',scaleX+1,-74,-22);
where mc is the display object.

For action script 3 i have found a blog from which i have found this idea , to know more about it click here .



No comments:

Post a Comment