您以令人困惑的方式使用“循环”一词。在编程中,循环通常指的是看起来像这样的“for”循环之一:
for (var i:int = 0; i < 10; i++)
{
//do stuff 10 times
}
我猜这不是你所说的循环,而是你想要一个MovieClip 或主时间线来减少n 帧末尾的Sound 对象的音量。或者你的意思是音乐本身在循环播放?希望你能看到提出写得很好的问题的价值。话虽如此..
请注意,我还没有尝试过,但根据我的参考书(Lott、Schall 和 Peters 的 ActionScript 3.0 Cookbook),您需要使用 SoundTransform 对象来指定您希望声音达到的音量被设置。试试这个:
var _sound:Sound = new Sound(music.wav); // creates a Sound object which has no internal volume control
var channel:SoundChannel = _sound.play(); // creates a SoundChannel which has a soundTransform property
var transform:SoundTransform = new SoundTransform(); // SoundTransform objects have a property called "volume". This is what you need to change volume.
现在在您的循环中(或您正在使用的帧事件中)执行以下操作:
transform.volume *= 0.9; // or whatever factor you want to have it decrease
//transform.volume /= 1.1; // or this if you prefer.
channel.soundTransform = transform; //
因此,只要您希望音量按此增量减少,请运行这段代码。当然,您需要确保您设置的任何变量都可以在引用它们的代码中访问。想到的一种方法是使用函数。
private function soundDiminish(st:SoundTransform, c:SoundChannel, factor:Number = 0.9):void
{
st.volume *= factor;
c.soundTransform = st;
}
现在,只要您想减小音量,只需调用 soundDiminish 函数即可。
也许你的框架事件是这样的:
function onLoadFrame(fe:Event):void
{
soundDiminish(transform, channel); // 3rd parameter optional
}
如果您只想每 20 帧调用一次此函数,则:
function onLoadFrame(fe:Event):void
{
// this is a counter that will count up each time this frame event happens
frameCount ++;
if (frameCount >= 20)
{
soundDiminish(transform, channel); // 3rd parameter optional
frameCount = 0; // reset the frame counter
}
}