2

我正在使用此代码使用该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;
}
4

1 回答 1

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
}
于 2013-03-13T15:55:38.097 回答