我只是一个奇怪的问题。我想把声音放在我的游戏中。这是一个有两个阶段的游戏。一个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无效
答案 0 :(得分:1)
所以, 我找到了解决问题的解决方案。显然,SDL_OpenAudio的返回值始终是成功或失败,而不是设备ID,这意味着您只能使用此功能一次打开一个设备。我刚刚在启动战斗时重新启动它之前关闭了音频,它确实有效。