I currently have a vertical slider that is controlled by the user using startDrag. The puck is restricted in movement by a track movieclip that is 115px high.
private function init():void
{
puck.y = (track.height-puck.height)/2;
puck.buttonMode = true;
puck.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
}
private function sendNewValue():void
{
trace(Math.round(puck.y-track.y+(puck.height/2)));
var newVal:Number = ; //need some math magic here
dispatchEvent(new ToolEvent(ToolEvent.SCALE,1));
}
private function onMouseDown(evt:MouseEvent):void
{
this.stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
puck.startDrag(false,new Rectangle((track.x + (track.width/2))-(puck.width/2),track.y-(puck.height/2),0,track.height));
}
private function onMouseUp(evt:MouseEvent):void
{
puck.stopDrag();
sendNewValue();
}
I can get a value from it using Math.round(puck.y-track.y+(puck.height/2)), which gives me values between 1 and 115 based on the position of the puck.
Now sadly maths is not my strong point at all (helpful for a programmer!), so would someone please explain how I can convert these values so that:
1 = 2
115 = 0
midpoint (115/2) = 1
Edit: kind of like the question here: How do I reverse my sound volume math for my volume slider?, but as well as inverting the value I need to adjust it on a scale.
if you’re looking for a linear function such that:
Generalization:
If you looking for a linear function f(x) = a.x + b such that:
then the solution is: