使用AS3中的空格键从库中播放声音

时间:2012-05-03 02:03:09

标签: actionscript-3

我一直试图想出一个简单的代码,每次按下空格键时都会从库中发出声音“跳”,但没有运气。我尝试的一切都将我的fla / swf变成闪光灯。

1 个答案:

答案 0 :(得分:1)

var spacebarDown:Boolean = false;

stage.addEventListener(KeyboardEvent.KEY_DOWN, _keyHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, _keyHandler);

function _keyHandler(e:KeyboardEvent):void
{
    if(e.keyCode == 32)
    {
        switch(e.type)
        {
            case KeyboardEvent.KEY_DOWN:

                if(!spacebarDown)
                {
                    spacebarDown = true;

                    // Play Sound.
                    var sfx:YourSound = new YourSound();
                    sfx.play();
                }

                break;


            case KeyboardEvent.KEY_UP: spacebarDown = false; break;
        }
    }
}