3 Replies Latest reply on May 4, 2008 4:12 PM by kglad

# linking sound volume to mouse distance?

I'm using Flash 8 actionscript 2.

I have multiple movie clips with sounds attatched to them. I want to have the volume of the clips linked to the proximity of the mouse, so that if the mouse is within 200 pixels of the button inside the movieclip, the volume of the sound will get louder, and vice versa. How do i do this??
• ###### 1. Re: linking sound volume to mouse distance?
use a loop (onMouseMove would work well here) to repeatedly poll the _xmouse and _ymouse properties, determine the distance between the mouse and whatever object you want (pythagoras' theorem is applicable) and convert that distance to a volume using a linear function of the distance to give a volume:

f(x) = ax+b;

f(0)=100;
f(200)=0;

solve for a and b -> b=100,a=-.5
• ###### 2. Re: linking sound volume to mouse distance?
sorry, i'm pretty new to this. this is my existing code inside the movie clip that changes the size according to proximity, so i have the distance tracked. where do i fit in the volume function?

this.onEnterFrame = function () {
a = _parent._ymouse - this._y;
b = _parent._xmouse - this._x;
dist = Math.sqrt((a * a) + (b * b));

if (dist < 200 ){
this._xscale = (200 - dist) + 30;
this._yscale = (200 - dist) + 30;

}
if (dist > 200){
this._xscale = 30;
this._yscale = 30;
}
}

thanks!
: