Actionscript 3 - 使声音仅在物体接触时播放一次

发布于 2024-10-19 09:37:56 字数 1101 浏览 4 评论 0原文

我有 griddle_mc、bacon_mc 和 BaconCooking.wav。我想在培根移到煎锅上时播放 wav 音乐。如果我移动培根直到它刚刚接触煎锅,则 wav 播放得很好,但如果我继续将 bacon_mc 拖过煎锅,则 wav 会变得响亮且失真,几乎就像它同时播放多个 wav 实例一样。

private function __handleSliceDown($evt:MouseEvent):void {
    slice1_mc.startDrag(true);
}

private function __handleSliceUp($evt:MouseEvent):void {
    slice1_mc.stopDrag();
    addEventListener(Event.ENTER_FRAME, __checkHit);     
}

private function __checkHit($evt:Event):void {
    if (this.slice1_mc.hitTestObject(griddle_mc)) {
        if (!hitting) {
            sc = BaconCooking.play();    
        }
    }
}

如何才能让wav正确播放?

编辑:

 private function __checkHit($evt:Event):void {
             if (this.slice1_mc.hitTestObject(griddle_mc)) {    
                if(isPlaying){
                    sc = BaconCooking.play(); 
                    isPlaying = true;
                }
             }else{
                 isPlaying = false;
                 sc.stop();
                    }
             }
  • isPlaying 是一个布尔值,默认设置为 false。

I have a griddle_mc, bacon_mc, and BaconCooking.wav. I want to play the wav when the bacon is moved onto the griddle. If I move the bacon until it just touches the griddle the wav plays fine, but if I continue to drag the bacon_mc across the griddle the wav becomes loud and distorted, almost like it's playing multiple instances of the wav at the same time.

private function __handleSliceDown($evt:MouseEvent):void {
    slice1_mc.startDrag(true);
}

private function __handleSliceUp($evt:MouseEvent):void {
    slice1_mc.stopDrag();
    addEventListener(Event.ENTER_FRAME, __checkHit);     
}

private function __checkHit($evt:Event):void {
    if (this.slice1_mc.hitTestObject(griddle_mc)) {
        if (!hitting) {
            sc = BaconCooking.play();    
        }
    }
}

How can I get the wav to play correctly?

EDIT:

 private function __checkHit($evt:Event):void {
             if (this.slice1_mc.hitTestObject(griddle_mc)) {    
                if(isPlaying){
                    sc = BaconCooking.play(); 
                    isPlaying = true;
                }
             }else{
                 isPlaying = false;
                 sc.stop();
                    }
             }
  • isPlaying is a boolean set to false by default.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_失温 2024-10-26 09:37:56

现在您已经有了 if(isPlaying){,但不应该是 if(!isPlaying){ 吗?

我的做法是:当两个对象命中时,如果 isPlaying 布尔值为 false,则将其设置为 true 并播放声音。仅当对象不再发生碰撞时,isPlaying 布尔值才会再次变为 false。

Now you have if(isPlaying){ but shouldn't that be if(!isPlaying){?

The way I would do it: when the two objects hittest, set the isPlaying boolean to true if it was false and play the sound. The isPlaying boolean can only become false again if the objects don't collide anymore.

烟燃烟灭 2024-10-26 09:37:56

编辑:由于 isPlaying 默认为 false,因此它永远不会开始播放声音。你的 if 语句应该是:

 if(!isPlaying){
 <code to play sound
 }

另外,我之前接触过这一点,但我将扩展:你需要一个额外的事件监听器来检测对象何时不接触,当调用它时,你会将 isPlaying 设置为 false。我已经有一段时间没有使用 Actionscript 进行编程了,但这就是您需要做的事情的总体思路。

使用变量和条件语句。每次物体碰撞时,它都会检查这个变量是否为真。如果是,那么它将播放声音并将变量设置为 true,否则,它不会执行任何操作。当对象不再接触时,该变量将设置为 false。

edit: Since isPlaying is false by default, than it will never play the sound to begin with. your if statement should be:

 if(!isPlaying){
 <code to play sound
 }

Also, I touched in this before, but I will expand: you need an additional event listener that detects when the objects are -not- touching, when that is called you would set isPlaying to false. It has been some time since I've programmed in Actionscript, but that is the general idea of what you need to do.

Use a variable and a conditional statement. Every time the objects collide, it would check if this variable was true. If it was, than it would play the sound and set the variable to true, otherwise, it would do nothing. When the objects were no longer touching, than the variable would be set to false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文