亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何確保指定聲音只有 1 個實例在循環以及如何阻止它循環?

如何確保指定聲音只有 1 個實例在循環以及如何阻止它循環?

一只斗牛犬 2022-11-30 17:02:19
我目前正在開發一款游戲,在播放器更新方法中,我希望腳步Sound在玩家行走時開始循環,我希望它在玩家停止行走時停止循環。但是,我無法弄清楚如何確保只有 1 個實例在Sound循環。澄清一下:我說的是gdx.audio.Sound課堂。這是我的代碼目前的樣子://Sets the footstep sound. i.e. it changes to a grass soundfile when you walk on grass.String footstepsFilePath = gameMap.getTileSoundFilePath(rect);setFootsteps(Gdx.audio.newSound(Gdx.files.internal(footstepsFilePath)));//velocity is the speed at which the player is going in the x or y direction.if(velocity.y != 0 || velocity.x != 0) footsteps.loop();if(velocity.y == 0 && velocity.x == 0) footsteps.stop();結果:當玩家開始移動時,大量腳步聲實例開始循環。當播放器停止移動時,所有播放器都會繼續循環。第一部分是出于顯而易見的原因,但我無法弄清楚如何確保只有一個實例在循環。但是對于第二部分,我不確定為什么不是所有的腳步聲實例都停止循環,因為這是文檔中stop()所說的:停止播放此聲音的所有實例。
查看完整描述

1 回答

?
largeQ

TA貢獻2039條經驗 獲得超8個贊

假設你if(velocity.y != 0 || velocity.x != 0)經常檢查,你確實會拉開很多循環。訣竅是檢查“玩家是否在移動,我上次看時他們還在嗎?” 而不僅僅是“玩家在移動”。


一種簡單的方法是設置一個布爾標志:


//Sets the footstep sound. i.e. it changes to a grass soundfile when you walk on grass.

String footstepsFilePath = gameMap.getTileSoundFilePath(rect);

setFootsteps(Gdx.audio.newSound(Gdx.files.internal(footstepsFilePath)));


boolean isMoving = false;


//velocity is the speed at which the player is going in the x or y direction.

if((velocity.y != 0 || velocity.x != 0) && !isMoving) {

    isMoving = true;

    footsteps.loop();

}


if((velocity.y == 0 && velocity.x == 0) && isMoving) {

    footsteps.stop();

    isMoving = false;

}

我不完全確定為什么stop在您的情況下不起作用。但是,其他兩個loop重載的文檔說明


您需要使用返回的 ID 調用 stop(long) 來停止聲音。


也許stop您正在使用的版本不起作用,或者它等待當前循環完成?


查看完整回答
反對 回復 2022-11-30
  • 1 回答
  • 0 關注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號