Positioning Based on Mouse Location
Download the example here.
Positioning based on mouse location can be used for a lot of different visual effects in Flash. Here is a breakdown of the equation commonly used to achieve such a task:
First determine a left most and right post position for your Object
The left most position is where the object should be when the mouse is all the way to the left of the stage. The right most position is where your Object should be when the mouse is at the right most position of the stage.
For this example we’ll use 0 for our leftMostPosition and the stageWidth (550) for our rightMostPosition.
Caclulate the distance between the two points
To calculate the distance between your leftMostPosition and rightMostPosition, simply subtract them like so:
distance = rightMostPoint – leftMostPoint
Calculate the Objects position based on the mouse
In order to calculate the position, we need to get a decimal number that represents where the mouse is in relation to the leftMost and rightMost points. Because we are using 0 and stageWidth for this example, we calcualte this distance like so:
mouseDecimal = mouseX / stageWidth
So if the mouse is at 0, mouseDecimal will be 0, if the mouse is at the stageWidth (far right) mouseDecimal will be 1, anywhere else, it will be somewhere inbetween 0 and 1.
To position our clip, we just multiply the distance by the mouseDecimal and add the leftMostPosition like so:
box.x = leftMostPosition + (mouseDecimal * distance);

