尝试在动画期间播放声音,但是动画的每一帧都播放一次声音

Trying to play sound during an animation, however the sound is played once for every frame of the animation

正如标题所暗示的那样,我正在尝试 运行 在某个动画播放时播放一段声音,我对其他声音没有任何问题,并且通过使用推导我得出结论,声音每循环一次动画帧经过的时间。应该是鲨鱼与地雷相撞,地雷爆炸,听到一声巨响,游戏结束。

编辑:Boom Sound 的代码仅在检测到碰撞时调用。当玩家与收藏品碰撞时还有另一种声音,但是 声音效果很好。只有当玩家与地雷发生碰撞并播放爆炸动画时,声音才会变得混乱。每次动画帧通过时它都会重新启动。

 if (Intersector.overlaps(this.mineC[i], this.sharkC)) {
            this.boomsound.play();
            this.collision = true;
            this.gamestate = 2;
            this.runtime += Gdx.graphics.getDeltaTime();
            this.game.batch.draw((TextureRegion) this.boom.getKeyFrame(this.runtime, false), (float) (Gdx.graphics.getWidth() / 5), this.sharkY - 120.0f);

            this.boom.setPlayMode(PlayMode.NORMAL);
            if (this.boom.isAnimationFinished(this.runtime)) {
                boomsound.stop();
                this.game.setScreen(new GameOverShark(this.game));
            }
        }

this.boomsound.play() 在代码的每次迭代中调用,您应该只调用一次

 // field
 private boolean boomNotStarted = true; 

 ....

 if (Intersector.overlaps(this.mineC[i], this.sharkC)) {
      if (boomNotStarted) {
            this.boomsound.play();
            this.boomNotStarted = false;
      }

      ...
 }