1

嗨,我对 actionscript 真的很陌生,我在播放声音时遇到了问题。当我松开向上键时,我希望我的声音停止播放。我设法让声音在按键时播放。

这是我的代码...

import flash.events.KeyboardEvent;

var mySound:Sound = new carstart();

stage.addEventListener(KeyboardEvent.KEY_DOWN, playSound);
stage.addEventListener(KeyboardEvent.KEY_UP, stopSound);

function playSound(keyEvent:KeyboardEvent) {
    if (keyEvent.keyCode == 38) {
        mySound.play();
    }
}

function stopSound(keyEvent:KeyboardEvent) {
    if (keyEvent.keyCode == 38) {
     mySound.stop();
    }
}

我得到这个错误

Scene 1, Layer 'Actions', Frame 1, Line 29  1061: Call to a possibly undefined method stop through a reference with static type flash.media:Sound.

如果您能想到任何事情,那将是一个很大的帮助。

谢谢

4

2 回答 2

1
import flash.events.KeyboardEvent;

var mySound:Sound = new carstart();
var myChannel:SoundChannel = new SoundChannel();

myChannel = mySound.play();

function playSound(keyEvent:KeyboardEvent) {
    if (keyEvent.keyCode == 38) {
        mySound.play();
    }
}

function stopSound(keyEvent:KeyboardEvent) {
    if (keyEvent.keyCode == 38) {
     myChannel.stop();
    }
}
于 2012-05-14T14:09:18.270 回答
0

您需要将您的Sound对象包装成一个SoundChannel... 按照这个示例,您将立即设置!

于 2012-05-14T14:09:42.140 回答