Skip to main content
Participant
May 4, 2008
Question

linking sound volume to mouse distance?

  • May 4, 2008
  • 1 reply
  • 295 views
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??
This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
May 4, 2008
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
Participant
May 4, 2008
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!
kglad
Community Expert
Community Expert
May 4, 2008
: