我只是一个奇怪的问题。我希望在我的游戏中加入声音。这是一个有两个阶段的游戏。一种是rpg,一种是战斗。在 rpg 中,您可以在踩到怪物时发起战斗。在我的代码中,我使用 3 次 SDL_mixer 来播放音乐(在菜单中、rpg 和战斗中)。它在前两种情况下有效,但是当我发起战斗时没有音乐。音乐已加载,Mix_playingMusic 返回 true,但在战斗开始时我听不到任何音乐。我使用与 rpg 和菜单中相同的音乐。SDL_Mixer 的代码到处都是一样的。战斗部分代码:
{
SDL_Event events;
Mix_Music *fightMusic;
enum actual_player actual = PLAYER;
int go = 1;
int action;
int monsterId = 0;
Uint32 t = SDL_GetTicks(), nt;
fightMusic = Mix_LoadMUS("data/Music/rpg.mp3"); // we loading the music
if(fightMusic == NULL){
printf("error loading fightMusic\n");
}
Mix_VolumeMusic(MIX_MAX_VOLUME);
if(Mix_PlayMusic(fightMusic, -1)<0){
printf("error playing fightMusic \n");
}
if (Mix_PlayingMusic() ){
printf("Music it's playing \n");
}
while(go){
if(isFinished(fightSdl->fight))
go = 0;
nt = SDL_GetTicks();
while (SDL_PollEvent(&events)){
action = inputKeyboard(events);
if(action == QUIT){
return -1;
go = 0;
}
if(action == -2){
clearBufferGame(fightSdl->buffer, CHARACTER);
}
if(actual == PLAYER) {
switch(action){
case MOVE_DOWNWARD:
addActionToBufferGame(fightSdl->buffer, DOWN);
break;
case MOVE_UPWARD:
addActionToBufferGame(fightSdl->buffer, UP);
break;
case MOVE_LEFT:
addActionToBufferGame(fightSdl->buffer, LEFT);
break;
case MOVE_RIGHT:
addActionToBufferGame(fightSdl->buffer, RIGHT);
break;
case ATTACK_1:
actionAttackSdl(fightSdl, ATTACK_1);
break;
case ATTACK_2:
if(Mix_PlayChannel(-1,fireSound,0)<0){
printf("error playing fire sound");
}
actionAttackSdl(fightSdl, ATTACK_2);
break;
case ATTACK_3:
actionAttackSdl(fightSdl, ATTACK_3);
break;
case NOTHING:
actual = ENNEMY;
break;
case -1:
setIsIdleCharacSdl(fightSdl->characSdl,1);
}
}
}
if (nt-t>300){
if(actual == ENNEMY){
hitNRunSdl(fightSdl, monsterId);
if(getMpEnnemyFight(fightSdl->fight) == 3 && getMpEnnemyFight(fightSdl->fight) == 3)
monsterId++;
if(monsterId >= fightSdl->nbMstrSdl){
actual = PLAYER;
setApPlayerFight(fightSdl->fight, 3);
setMpPlayerFight(fightSdl->fight, 3);
monsterId = 0;
}
}
t = nt;
}
// updatePlayerDisplay(fightSdl);
// updateMonsterDisplay(fightSdl);
updatePlayerActionFight(fightSdl);
updateSpellDisplay(fightSdl);
updateDisplay(fightSdl);
drawGame(fightSdl);
// SDL_RenderPresent(fightSdl->renderer);
}
if(getLifePointsCharacter(getCharacterFight(fightSdl->fight)) >= 0)
return 1;
return 0;
}
编辑:我添加了一个printf("%s", Mix_GetError)
并且菜单和 rpg 中没有错误,但在战斗中它是打印的:无效的音频设备 ID