AS3:鼠标速度影响音量

时间:2013-03-13 12:28:51

标签: actionscript-3

我正在使用此代码使用ENTER_FRAME功能检测鼠标的速度。鼠标的速度会影响我在循环播放的mp3。通过使用ENTER_FRAME,它按帧速率进行更新,并使音乐声音变得非常结实和混乱。

有没有办法放松音量(仍然使用ENTER_FRAME)或者我的mp3会不会受到影响?

var mySound1:Sound = new Sound();
mySound1.load(new URLRequest("AStringsMinor.mp3"));

var myChannel1:SoundChannel = new SoundChannel();
var myTransform1:SoundTransform = new SoundTransform();
myChannel1 = mySound1.play(0, 999);

var prevPt:Point = new Point(mouseX, mouseY); 

addEventListener(Event.ENTER_FRAME, mouseSpeed); 

function mouseSpeed(e:Event)
{     
    var xspeed:Number = Math.abs(mouseX - prevPt.x);     
    var yspeed:Number = Math.abs(mouseY - prevPt.y);    
    var speed:Number = (xspeed + yspeed) / 100;
    prevPt = new Point(mouseX, mouseY);    

    myChannel1.soundTransform = myTransform1;
    myTransform1.volume = speed;
}

1 个答案:

答案 0 :(得分:2)

我相信你应该通过平均约5秒的鼠标速度来缓解你的速度变化。

var speeds:Vector.<Number>;
var spn:int; // how many are there
var spi:int; // current index
// into initialization code:
spn=Math.ceil(5.0*stage.frameRate); // 5.0 is a number of seconds to average
speeds=new Vector.<Number>(spn,true);
for (spi=spn-1;spi>=0;spi--) speeds[spi]=0; // explicit init
spi=0;
// the function gets updated
function mouseSpeed(e:Event)
{     
    var xspeed:Number = Math.abs(mouseX - prevPt.x);     
    var yspeed:Number = Math.abs(mouseY - prevPt.y);    
    var speed:Number = (xspeed + yspeed) / 100;
    prevPt = new Point(mouseX, mouseY);    
    speeds[spi++]=speed; // record current speed
    if (spi==spn) spi=0; // wrap
    speed=0;
    for (var n:int=spn-1;n>=0;n--) speed+=speeds[n]; // sum all recorded speeds
    myChannel1.soundTransform = myTransform1;
    myTransform1.volume = speed/spn; // and now average it
}