【问题标题】:Why my button not work correctly? SDL2为什么我的按钮不能正常工作? SDL2
【发布时间】:2016-10-12 13:02:07
【问题描述】:

我尝试用 SDL2 和 C++ 编写小程序。窗口上有一个窗口和按钮。当我第一次按下按钮音乐开始播放时,我想这样做。是的,我成功了。

            if((mouseX >= buttonRect.x && mouseX < (buttonRect.x + buttonRect.w)) && (mouseY >= buttonRect.y && mouseY < (buttonRect.y + buttonRect.h))){
                if(event.button.button == SDL_BUTTON(SDL_BUTTON_LEFT)){
                    if(Mix_PlayingMusic() == 0){
                        Mix_PlayMusic(music, -1);
                    }
                }
            }

但我想在第二次按下按钮时让音乐暂停。如果它再次按下它会恢复。

我试过了:

            if((mouseX >= buttonRect.x && mouseX < (buttonRect.x + buttonRect.w)) && (mouseY >= buttonRect.y && mouseY < (buttonRect.y + buttonRect.h))){
                if(event.button.button == SDL_BUTTON(SDL_BUTTON_LEFT)){
                    if(Mix_PlayingMusic() == 0){
                        Mix_PlayMusic(music, -1);
                    }else{
                        if(Mix_PausedMusic() == 1){
                            Mix_ResumeMusic();
                        }else{
                            Mix_PauseMusic();
                        }
                    }
                }
            }

但这行不通。当我按下按钮时,音乐立即开始和停止。如果我按住鼠标按钮,音乐就会播放,但如果我移动鼠标,它就会停止。

【问题讨论】:

    标签: c++ sdl-2 audio-player


    【解决方案1】:

    Mix_PlayMusic 成功返回 0,错误返回 -1。 尝试定义静态变量来跟随播放器的状态;

                if((mouseX >= buttonRect.x && mouseX < (buttonRect.x + buttonRect.w)) && (mouseY >= buttonRect.y && mouseY < (buttonRect.y + buttonRect.h))){
                    static int STATE = 0; // not played
                    if(event.button.button == SDL_BUTTON(SDL_BUTTON_LEFT)){
                        if(STATE == 0){
                            Mix_PlayMusic(music, -1);
                            STATE = 1; // playing music
                        }else{
                            if(STATE == 2){
                                Mix_ResumeMusic();
                            } else {
                                Mix_PauseMusic();
                                STATE = 2; // paused
                            }
                        }
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 2023-02-02
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多