我正在写一个游戏,玩家在挑选硬币。捡硬币应该有声音,我使用PlaySound播放* .wav文件。有用。但是有时(7/10次尝试)应用程序会崩溃。我不知道为什么有时候行之有效,有时却行不通。
有趣的是,当我在调试模式下运行应用程序时,它运行正常
是什么原因引起的问题,如何解决?
您还能推荐另一种播放WAV的方法吗? (就这么简单(使用一两个功能))
我尝试使用PlaySoundA,sndPlaySound的所有变体-相同的结果
试图直接从文件读取wav并将其保存在内存中-相同的结果
//This sample checks if there a coin on the right
void Player::step_right() {
if (check_right()) {
sndPlaySound(TEXT("D:\\projects\\s.wav"), SND_FILENAME | SND_ASYNC);
//PlaySoundA(TEXT("D:\\projects\\s.wav"), nullptr, SND_FILENAME | SND_ASYNC);
//sndPlaySound(buffer, SND_MEMORY || SND_ASYNC);
netWorth++;
steps+=10;
}
cur->X++;
}
upd: 我使用MinGW
挤压后控制台输出:进程结束,退出代码-1073741819(0xC0000005)
这是Player类的规范:
class Player {
private:
Point* cur; // Current coords
Point post; // Last coords
int steps; // Remaining steps
int netWorth; // Picked coins
void (Player::*m[4])(); // Array of movement functions pointers
// Movement
void step_right();
void step_left();
void step_up();
void step_down();
// Coins checking
bool check_right();
bool check_left();
bool check_up();
bool check_down();
public:
Player();
Point* Getcur() { return cur; }
void Move(void (Player::*t)());
bool isOutofSteps();
auto ReturnSomeArray() { return m; }
int GetNet(); //Networth
void display_player_info();
};
我也有具有
的Action_Listener类map<int, void (Player::*)()> m;
根据键码,我向void Player::Move(void (Player::*t)())
发送函数指针
此处:
if (m.find(event) != m.end() && !player->isOutofSteps()) {
void (Player::*t)() = m[event];
player->Move(t);
unsigned int (Drawer::*y)() = drawhandlers[event];
drawer->Draw_Hero(y);
player->display_player_info();
} else if(player->isOutofSteps()){
terminal_print(2, 2, "You're out of steps");
return;
}
移动功能:
void Player ::Move(void (Player::*t)()) {
post = *cur;
(this->*t)();
steps--;
}