主题:  关于用距离控制声音

alalala

职务:普通成员
等级:1
金币:2.0
发贴:64
注册:2000/12/18 14:07:27
#12003/8/6 12:34:13
只是想为多媒体的发展尽一份力。以下是翻译director online 上的一篇关于用距离控制声音的文章,觉得简单有用,所以给大家共享。动机单纯。-------alalala


提问
我需要将一张图片放到舞台的中央,以便鼠标靠近的时候声音会马上淡入,同时图片做淡入淡出的发光效果


回答
还记得高中学过的几何学如何计算直角斜边的长度吗?我们来回顾一下。假设你知道了两条直角边x,y。计算第三条边的长度的公式是

X2 + Y2 = Z2

很容易得出z的长度始终大于x或y的长度

现在我们把它应用到你问题中去。设想一下直角边的斜边z由你的精灵的坐标和鼠标的坐标来决定。x是你的精灵和鼠标的水平距离,y是你的精灵和鼠标的垂直距离。现在,以上的公式可改为

set x = the mouseH - the locH of sprite whatever
set y = the mouseV - the locV of sprite whatever
x2 + y2 = the distance2

这就是我们要用的概念。因为距离永远不可能为负数,当我们编写lingo的时候,我们使用绝对值属性来设定x和y。同时,我亲向于使用描述性的变量来定义x,y。在这个例子中我将使用hinc和vinc来表示“水平增量和垂直增量”

接下来的步骤就是是这个距离和以下的事件关联:1)声音通道的音量。2)精灵的混合值(发光的精灵)。音量的范围是从0到255,既然我们希望声音在鼠标接近的时候增加,我们可以简单的制定音量等于255减去距离。为了增强可操作性,我创建了“fade"变量。我们使用“fade"变量来乘以距离,这样我们可以使音量在较短的距离里增减。(fade的值越大,变化的越快)

最后,你需要改变墨水的混合值,来实现发光的效果。在这个行为里,我们让这个混合值与音量关联。混合值由1到100,为了使它适应音量的0到255的变化范围,我们设定混合值为 数值/256。


以下便是这个行为。你需要制定一下发光的精灵。

property pMyLocH
property pMyLocV
property pFade
property pSoundChannel
property pMySprite

on getPropertyDescriptionList me
set pdlist to [:]
set the floatPrecision to 1
addprop pdlist, #pFade, [#comment:"Rate of fade", #format:#float, #default:3.0]
addprop pdlist, #pSoundChannel, [#comment:"Which sound channel", #format:#integer, #default:1]
return pdlist
end getPropertyDescriptionList

on beginSprite me
set pMySprite = the spriteNum of me
set pMyLocH = the locH of sprite pMySprite
set pMyLocV = the locV of sprite pMySprite
set the blend of sprite pMySprite to 0
end

on exitframe me
set hInc = abs(the mouseH - pMyLocH)
set vInc = abs(the mouseV - pMyLocV)

set distance = sqrt((hInc * hInc) + (vInc * vInc))
set the volume of sound pSoundChannel = 255 -(distance * pFade)
set the blend of sprite pMySprite = the volume of sound pSoundChannel/2.56
end

编辑历史:[这消息被alalala编辑过(编辑时间2003-08-06 17:58:27)]


HolyKnight

职务:普通成员
等级:1
金币:1.0
发贴:175
注册:2002/12/25 12:38:14
#22003/8/6 16:52:59
很有意思我会试试的



古剑刀

职务:普通成员
等级:1
金币:0.0
发贴:41
注册:2004/6/24 20:55:05
#32004/8/10 11:29:37
还不错的算法



草原风

职务:版主
等级:5
金币:13.0
发贴:3489
注册:2001/2/17 19:47:06
#42004/8/11 16:53:55
不错,希望继续发表类似的文章



cherub

职务:普通成员
等级:1
金币:0.0
发贴:105
注册:2004/8/6 1:03:24
#52004/8/13 22:58:58
这方法不错!