【问题标题】:Trying to play sound during an animation, however the sound is played once for every frame of the animation尝试在动画期间播放声音,但动画的每一帧都会播放一次声音
【发布时间】:2020-01-13 05:11:53
【问题描述】:

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

编辑:只有在检测到碰撞时才会调用 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));
            }
        }

【问题讨论】:

    标签: android animation audio libgdx


    【解决方案1】:

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

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

    【讨论】:

    • 我将如何实现此代码? NotStarted 似乎不是 libgdx 中的一个东西,而且我的代码也不是最好的。
    • 这是一个伪代码,可让您大致了解问题所在。我编辑了代码并使其可执行。这是非常可怕的实现,但我不能给你更多,因为我对你的程序了解不多。如果您需要多个繁荣事件,编辑后的代码将无法正常工作,并且可能会出现更多问题
    • 我现在明白了,抱歉我在可视化代码时遇到了问题。但是我将您编辑的代码放入了我的程序中,它现在可以工作了。非常感谢你
    猜你喜欢
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多