我正在构建一个基于文本的 RPG,并且我已经成功地将一些.wav
文件添加到我的程序中,没有问题,我也能够正常播放它们,也没有问题。
目前会发生什么?
我有 2 个.wav
文件,1 个用于一般背景音乐 ( newbieMelody.wav
),另一个用于升级时 ( levelUp.wav
)。因此,首先,当我运行我的游戏时,我们从newbieMelody.wav
要在后台播放的文件开始,如下所示:
#pragma comment(lib, "winmm.lib")
#include "Quiz.h"
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
int main() {
PlaySound(TEXT("C:\\Users\\User\\Documents\\GIT\\TextBased\\QuizApplication\\playSound\\newbieMelody.wav"),
NULL, SND_FILENAME | SND_ASYNC | SND_LOOP); // we play the sound here
// create a game
Game *game = new Game();
game->readAreaFromFile(); // calls a file for area location
game->run(); // loads the game UI
//destroy gameWorld
delete game;
system("PAUSE");
}
然后,每当我们在我的 void Game::run() 函数(在另一个 cpp 文件中)中升级时,我都会做一些增量、重置xpCount
并播放levelUp.wav
:
if (xpCount >= xpPeak) { // if current exp is larger or = to xp cap
combatLevel += 1; // current combat lvl
xpCount = 0; // records total exp
xpPeak += 4; // takes longer to level up each level
setcolor(yellow, black); // sets location green
cout << "Congratulations, you just advanced your Combat level!" << endl;
cout << "Your Combat level is now " << combatLevel << "." << '\n' << endl;
PlaySound(TEXT("C:\\Users\\User\\Documents\\GIT\\TextBased\\QuizApplication\\playSound\\levelUp.wav"),
NULL, SND_FILENAME | SND_ASYNC); // play level up sound
this_thread::sleep_for(chrono::seconds(5)); // sleeps output to allow level up song to finish
levelUpDone = true; // leveled up
}
一旦完成播放,我们“恢复”播放newbieMelody.wav
文件:
if (levelUpDone == true) { // access since player leveled up
PlaySound(TEXT("C:\\Users\\User\\Documents\\GIT\\TextBased\\QuizApplication\\playSound\\newbieMelody.wav"),
NULL, SND_FILENAME | SND_ASYNC | SND_LOOP); // resume newbie melody???
levelUpDone = false; // back to false to avoid errors
}
有什么问题?
这通常可以按我的意愿工作,但是当我newbieMelody.wav
的文件在文件之后再次播放时levelUp.wav
,它会从头开始重新启动,而不是从上次播放的位置恢复。
我的问题是,我究竟如何能够更改我的代码,以便背景音乐从上次播放的位置恢复,而不是重新开始?有什么方法可以在levelUp.wav
文件播放时简单地暂停背景歌曲,然后在完成后恢复?