3 Replies Latest reply on Jun 29, 2008 8:41 PM by Newsgroup_User

# Volume linked to cursor proximity

Hi

What I want to achieve is that the cursor (or a sprite controlled by cursor keys) dictates the volume of a soundfile by it's proximity to a 'place' sprite, i.e. when the cursor gets within a certain radius of the sprite it starts the soundfile playing and then as it gets nearer it gets louder.

Not being good these things I would normally get the place sprite to test for whether the cursor is near it by checking above, below and on each side. However, I presume there must be a basic trigonometric thing where I can work out the radius and use that. Any ideas on this or a better approach would be appreciated.

Many thanks

Bob
• ###### 1. Re: Volume linked to cursor proximity
property spriteNum

on exitFrame me

-- calculates the distance between the cursor
-- and the registration point, using
-- Pythagorean theorem:
-- a squared plus b squared equals c squared

a = abs(sprite(spriteNum).locH - the mouseH)
b = abs(sprite(spriteNum).locV - the mouseV)
c = sqrt (a * a + b * b)

put c

end
• ###### 2. Re: Volume linked to cursor proximity
> a = abs(sprite(spriteNum).locH - the mouseH)
> b = abs(sprite(spriteNum).locV - the mouseV)

FWIW, there's no need to take the absolute value since when squared the
result will always be positive (ignoring imaginary numbers...)

> c = sqrt (a * a + b * b)
• ###### 3. Re: Volume linked to cursor proximity
Sean Wilson wrote:
>> a = abs(sprite(spriteNum).locH - the mouseH)
>> b = abs(sprite(spriteNum).locV - the mouseV)
>
> FWIW, there's no need to take the absolute value since when squared the
> result will always be positive (ignoring imaginary numbers...)
>
>> c = sqrt (a * a + b * b)

True, good point. Given that this is executing on every exitFrame event,
one less function call could improve performance. So

----------------------
property spriteNum

on exitFrame me

-- calculates the distance between the cursor
-- and the registration point, using
-- Pythagorean theorem:
-- a squared plus b squared equals c squared

a = sprite(spriteNum).locH - the mouseH
b = sprite(spriteNum).locV - the mouseV
c = sqrt (a * a + b * b)

put c

end
-----------------

Bob, you should be able to replace the "put c" line with your own code
to adjust the volume depending on the value of c.