Ultrashock Forums > Archived Forums > Sound
Sound volume = mouse proximity?

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
Sound volume = mouse proximity?
Old 2006-08-12

Hi guys,

I have this mc in my movie, and I want the volume of a certain sound to increase/fade up as the cursor gets closer to the mc, and also want it to decrease/fade down as the cursor gets away from the mc.

I've got this code:

Code:
onClipEvent (load) {
	s_jam = new Sound(this);
	s_jam.attachSound("jam");
	s_jam.start(0, 999);
	s_jam.setVolume(0);
	startVol = 100;
}
onClipEvent (enterFrame) {
	xSide = _root._xmouse-this._x;
	ySide = _root._ymouse-this._y;
	hypo = Math.sqrt((xSide*xSide)+(ySide*ySide));
	if (hypo<100) {
		if (jamStopped == true) {
			s_jam.start(0, 999);
			jamStopped = false;
		}
		deltaV = startVol-(startVol*(hypo*.01));
	} else {
		deltaV = 0;
		s_jam.stop();
		jamStopped = true;
	}
	s_jam.setVolume(deltaV);
}
But it seems pretty archaic, since I think it was done for flash 5, and also I want to be able to customize the distance at which the sound starts to fade.

Does anybody know someway of doing it, or how to fix this code?
postbit arrow 4 comments | 830 views postbit arrow Reply: with Quote   
Registered User
kindoki is offline
seperator
Posts: 6
2006-08-10
seperator

Ultrashock Member Comments:
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2006-08-12 #2 Old  
ActionScript Code:
  1. function getVolume( target:MovieClip, maxDistance:Number ):Number {
  2.     var vol:Number = 100 - ((getDistance(target)/maxDistance) * 100 >> 0);
  3.     if (vol < 0) {
  4.         vol = 0;
  5.     }
  6.     return vol;
  7. }
  8.  
  9. function getDistance( target:MovieClip ):Number {
  10.     var pos:Object = getGlobalPosition(target);
  11.     var mX:Number = _root._xmouse - (target._width / 2);
  12.     var mY:Number = _root._ymouse - (target._height / 2);
  13.     var x:Number = mX - pos.x;
  14.     var y:Number = mY - pos.y;
  15.     return Math.sqrt((x * x) + (y * y));
  16. }
  17.  
  18. function getGlobalPosition( target:MovieClip ):Object {
  19.     var x:Number = target._x;
  20.     var y:Number = target._y;
  21.     var mc:MovieClip = target._parent;
  22.     while (mc) {
  23.         x += mc._x;
  24.         y += mc._y;
  25.         mc = mc._parent;
  26.     }
  27.     return {x:x, y:y};
  28. }
To get the volume you would do something like this:

ActionScript Code:
  1. var volume:Number = getVolume(_root.myMovieClip, 100);
  2. mySound.setVolume(volume);
getVolume( targetMovieClip, maxDistance )

maxDistance is the distance where the volume will be zero. So, if you want the volume to be zero if the mouse is futher than 50px away from the movie clip, then you would pass 50 as the maxDistance value.

Hope that helps.
Reply With Quote  
kindoki kindoki is offline 2006-08-12 #3 Old  
Sorry, I'm pretty lame at actionscript.

Is that code supposed to be inserted in the timeline? And after this?

Code:
this.onLoad = function () {
	s_jam = new Sound(this);
	s_jam.attachSound("jam");
	s_jam.start(0, 999);
	s_jam.setVolume(0);
	startVol = 100;
}
And what about this piece of code?

Code:
var volume:Number = getVolume(_root.myMovieClip, 100);
mySound.setVolume(volume);
Is that to use also in the timeline after all the other code?

And:

Code:
getVolume( targetMovieClip, maxDistance )
That's when I want to call the function right?
So if my mc is called "ball", and if I want the sound to go off at 250 pix, it should be:

Code:
getVolume( ball, 250 )
Right?

There is also something wrong with the code cause Flash pops up this message:

**Error** Scene=Scene 1, layer=jammed full, frame=1:Line 1: '{' expected
function getVolume( target:MovieClip, maxDistance:Number ):Number {

**Error** Scene=Scene 1, layer=jammed full, frame=1:Line 7: Unexpected '}' encountered
}

Total ActionScript Errors: 2 Reported Errors: 2
Sorry for being so lame. And thank you for your help.
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2006-08-12 #4 Old  
You can just put that code into the first frame of your movie ( i.e. the main time line ).

When you want to update the volume of a sound you simply call the getVolume function, so you could use onEnterFrame:

ActionScript Code:
  1. this.onEnterFrame = function():Void {
  2.    var newVolume = _root.getVolume(ball, 250);
  3.    s_jam.setVolume(newVolume);
  4. }
I think you are getting the error because this code is AS2, so make sure that you have your publish settings set to ActionScript 2.0

Reply With Quote  
kindoki kindoki is offline 2006-08-12 #5 Old  
Last edited by kindoki : 2006-08-12 at 08:37.
It works!



I created a new .fla and if I use this code in the first frame of the main timeline of scene 1, it works. But if I use the code in the second frame in the same timeline it no longer works. Is it really necessary to use the code on the first frame of the main timeline? I'm asking this because I want to use this in my project, this code is supposed to be used on the second frame of a movie clip inside another movie clip on the main timeline, and it doesn't work. Is there anyway to solve this issue?

Thank you very much for your help



EDITh I'm using attachSound BTW, is that what's causing the problem?
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: