繁体   English   中英

为什么我的按钮不能正常工作? SDL2

[英]Why my button not work correctly? SDL2

我尝试用 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();
                        }
                    }
                }
            }

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

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
                        }
                    }
                }
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM