The version of this project previously discussed uses a fairly crude technique to adjust sound volume and typographic features: the code determines if the cursor length falls into one of four ranges, each of which corresponds to a fixed amount of adjustment. As the cursor moves, volume, size, and alpha change in stepwise fashion, making discrete jumps up or down. This approach lacks subtlety.
The demo above shows two approaches that are somewhat more sophisticated. In each, the sound and text adjustments occur smoothly, not stepwise: every time the cursor moves, there is some change.
Both examples use the same code to calculate the distance of the cursor from the target clip, represented by the variable hypo:
xSide = _root._xmouse - this._x; ySide = _root._ymouse - this._y; hypo = Math.sqrt((xSide * xSide)+(ySide * ySide));
This code is identical to its counterpart in the original example.
Soundbite A: Percentage
In the first example above (Soundbite A), constant variation is achieved by setting up a relationship in which the volume of the sound attached to the clip is decreased by a percentage corresponding to the cursor distance. This approach requires setting a variable called startVol which holds the initial sound volume. This variable is set in a load handler.
The following statement calculates the amount of volume change:
deltaV = startVol - (startVol*(hypo*.01));
Multiplying the cursor distance hypo by .01 converts it into a percentage, which is then both multiplied by startVol and subtracted from startVol. This change factor is applied thus:
this._width = startWidth + deltaV; this._height = startHeight + deltaV; this._alpha = startVol + deltaV;
Here's how all the math works in practice: The variable startVol contains the maximum volume setting. Each time the cursor moves, we calculate a second number that represents some percentage of startVol and take that much away. So as the cursor distance approaches 100 pixels, the amount subtracted approaches 100%. At 100 pixels, volume reaches zero.
Note: I've omitted code that shuts the sound off when volume hits zero. You must include this feature because settings below zero are read as positive, which means that the volume will increase again once distance passes 100 pixels. For the full script, download the .fla file from Crow (see below).
Soundbite B: Geometrical Variation
The percentage strategy works nicely if you want to limit the effect to a 100-pixel range. But what if you want to work on a larger canvas? In that case, consider Soundbite B. Here we use a different mathematical strategy:
deltaV = startVol*(1/Math.sqrt(hypo)); if(deltaV > startVol*.30) deltaV = startVol; if(deltaV < startVol*.08) deltaV = 0); s_believe.setVolume(deltaV);
In this case we multiply the starting volume (again, set in a load handler) by a fraction: one divided by the square root of the distance. Use of the square root produces a geometrical rather than a linear progression, meaning that the volume increases by greater amounts as we get closer.
Strictly speaking you may omit lines 2 and 3 of this script: they override the geometrical progression when the cursor exceeds certain limits. On the near end, they push volume to 100% once it has reached 30%. Without this refinement, the sound will reach full volume only when the cursor is on or very near the registration point of the movie. The outward limit turns the volume all the way down once the cursor reaches a certain distance. Without this addition, the sound would always be (maddeningly) audible at a very low level.
The source file for this demo is called noisyRoomImproved.fla and it is available on Crow in multimediaShare/noisyRoomImproved.
