scale movieclip relative to another position as3 matrix
Posted: Sat Nov 27, 2010 6:00 pm
scaling a movieclip with it's scale done with an origin at it's top left side as origin can be very hard to do in relation to another point on the flash stage. initially it maybe thought that you simply use a slider and scale that way up and down from 0-100.
you cannot scale something to 0, in most formulas that is division by zero and is a mathematical error in computers. 1-100 is better. The example I am going to list isn't perfect, but it shows that you must compare the scale previous to the scale present (request) as so:
you scaled an item to 50%. so, something 100 pixels tall is now 50.
how do you scale it back to 100%? You cannot multiply prop/100 (100% is 100/100) that would just be 1.
going from 50 to 100 is (100/50) or 2x larger than 50%. so record everytime you scale oldScale and then use your slider newScale request to see the proportional change.
factor=newScale/oldScale
zip examples v2 | v3
http://www.supercala.net/tuts/as3/2d/translatev2v3.zip
translatev3.fla root timeline code (not a class)
you cannot scale something to 0, in most formulas that is division by zero and is a mathematical error in computers. 1-100 is better. The example I am going to list isn't perfect, but it shows that you must compare the scale previous to the scale present (request) as so:
you scaled an item to 50%. so, something 100 pixels tall is now 50.
how do you scale it back to 100%? You cannot multiply prop/100 (100% is 100/100) that would just be 1.
going from 50 to 100 is (100/50) or 2x larger than 50%. so record everytime you scale oldScale and then use your slider newScale request to see the proportional change.
factor=newScale/oldScale
zip examples v2 | v3
http://www.supercala.net/tuts/as3/2d/translatev2v3.zip
translatev3.fla root timeline code (not a class)
Code: Select all
//-----
function start_drag_flux(e:MouseEvent):void
{
tgtMC.startDrag();
}
//-----
function stop_drag_flux(e:MouseEvent):void
{
tgtMC.stopDrag();
}
//-----
function scale_change(e:Event):void
{
var scaleAmt=Number(e.target.value);
var factor=Number(scaleAmt/100);
//
if(oldScale!=0)
{
newScale=scaleAmt/oldScale;
}
else
{
newScale=factor;
}
//remember for the next relation to another scale call
oldScale=scaleAmt;
var fluxOrigX=Number(tgtMC.x);
var fluxOrigY=Number(tgtMC.y);
pt_orig.x=fluxOrigX;
pt_orig.y=fluxOrigY;
var intersectX=Number(mc_origin1.x+(mc_origin1.width/2));
var intersectY=Number(mc_origin1.y+(mc_origin1.height/2));
pt_center.x=intersectX;
pt_center.y=intersectY;
var intersectDistX=Number(intersectX-fluxOrigX);
var intersectDistY=Number(intersectY-fluxOrigY);
pt_interDist.x=intersectX-intersectDistX;
pt_interDist.y=intersectY-intersectDistY;
tgtMC.scaleX=tgtMC.scaleY=factor;
var newDistX=intersectDistX*newScale;
var newDistY=intersectDistY*newScale;
tgtMC.x=intersectX-newDistX;
tgtMC.y=intersectY-newDistY;
}
//-----
pt_orig.tF_A.text="pt_orig";
pt_interDist.tF_A.text="pt_interDist";
var oldScale=Number(0);
var newScale=Number(0);
var tgtMC=mc_brussels1;
slider_scale.addEventListener(Event.CHANGE,scale_change);
tgtMC.addEventListener(MouseEvent.MOUSE_DOWN,start_drag_flux);
tgtMC.addEventListener(MouseEvent.MOUSE_UP,stop_drag_flux);