View Full Version : globalToLocal for scale and rotation?
One thing I like with AS3 is you can remove a child from one movieclip and add it to another. But the thing is, I want a way to do that but have it stay in the same place on screen, even though its parents are in different places.
Now, I can use localToGlobal and globalToLocal to translate a particular point from one context to the other, but I'm wondering if there's a way to do the same thing with the scale and rotation properties.
Okay. My solution:
package switcher
{
import flash.display.Sprite;
import flash.geom.Point;
import flash.display.DisplayObjectContainer;
public class switcher extends Sprite
{
public function switcher () {
}
public function switchParentTo (newParent:DisplayObjectContainer):void {
var oldParent:DisplayObjectContainer = parent;
var newpoint:Point = new Point();
newpoint.x=0;
newpoint.y=0;
newpoint = newParent.globalToLocal(this.localToGlobal(newpoin t));
while (oldParent!=root) {
rotation+=oldParent.rotation;
scaleX*=oldParent.scaleX;
scaleY*=oldParent.scaleY;
oldParent = DisplayObjectContainer(oldParent.parent);
}
newParent.addChild(this);
while (newParent!=root) {
rotation-=newParent.rotation;
scaleX/=newParent.scaleX;
scaleY/=newParent.scaleY;
newParent = DisplayObjectContainer(newParent.parent);
}
x=newpoint.x;
y=newpoint.y;
}
}
}
But I realized while making this that you can't logically translate scale in all instances; so this will work only as long as xscale and yscale are equal.
hm can't you impliment xscale * sin(rotation), etc per iteration to solve that problem?
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.