Augh!
Flash and Actionscript developing requires some Math tips often.It's useful to give best result at our animations and lift up the user interactivity.
In this sample i want keep the attraction son the Math proportions applied in Actionscript 3.0.
As we know, the proportion law is:
x:10=2:20
and we can get the x value by using inverse formulas.
By replacing the the formula's values with the MovieClip properties we need in our application, we get this:
The Class i have wrote is the following:
Code:
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.geom.Rectangle;
public class Balance extends MovieClip
{
private var offSet:Number;
private var start_1:Number;
private var start_2:Number;
private var my_rect:Rectangle;
public function Balance()
{
init();
}
public function init():void
{
stage.frameRate=31;
//handler_mc.buttonMode=true;
handler_mc.useHandCursor=true;
bg_mc.x=handler_mc.x;
bg_mc.y=handler_mc.y;
handler2_mc.x=bg2_mc.x;
handler2_mc.y=bg2_mc.y;
handler3_mc.x=bg3_mc.x;
handler3_mc.y=bg3_mc.y;
offSet=bg_mc.height-handler_mc.height;
start_1=handler2_mc.x;
start_2=handler3_mc.x;
my_rect=new Rectangle(bg_mc.x,bg_mc.y,0,offSet);
initEvents();
}
public function initEvents():void
{
handler_mc.addEventListener(MouseEvent.MOUSE_DOWN,mouseIsDown);
stage.addEventListener(MouseEvent.MOUSE_UP,mouseIsUp);
}
public function mouseIsDown(event:Event):void
{
handler_mc.startDrag(false,my_rect);
handler_mc.addEventListener(Event.ENTER_FRAME,applyBalance);
}
public function mouseIsUp(event:Event):void
{
handler_mc.stopDrag();
handler_mc.removeEventListener(Event.ENTER_FRAME,applyBalance);
}
public function applyBalance(event:Event):void
{
var p:Number=handler_mc.y-bg_mc.y;
var percentage:Number=Math.ceil((p/offSet)*100);
handler2_mc.x=start_1+(percentage/100)*(bg2_mc.width-handler2_mc.width);
handler3_mc.x=start_2+(percentage/100)*(bg3_mc.width-handler3_mc.width);
circle_mc.rotation=(percentage/100)*360;
}
}
}
Have fun!