我有一个简单的脚本,当鼠标悬停在第一个初始按钮上时,我希望脚本加载第二个按钮。但它不会返回 true,所以我似乎无法让它工作。
这是我的班级检查情况:
class Button
{
private:
int m_x, m_y;
int m_width, m_height;
public:
Button(int x, int y, int width, int height)
{
m_x = x;
m_y = y;
m_width = width;
m_height = height;
}
bool IsIn( int mouseX, int mouseY )
{
if (((mouseX > m_x) && (mouseX < m_x + m_width))
&& ((mouseY > m_y) && (mouseY < m_y + m_height ) ) ) {
return true;
} else {
return false;
}
}
void Render(SDL_Surface *source,SDL_Surface *destination)
{
SDL_Rect offset;
offset.x = m_x;
offset.y = m_y;
offset.w = m_width;
offset.h = m_height;
source = IMG_Load("button.png");
SDL_BlitSurface( source, NULL, destination, &offset );
}
};
它是IsIn
我试图开始工作的功能......在我的主循环中,我有:
while(!quit){
while( SDL_PollEvent( &event ) )
switch( event.type ){
case SDL_QUIT: quit = true; break;
case SDL_MOUSEMOTION: mouseX = event.motion.x; mouseY = event.motion.y; break;
}
Button btn_quit(screen->w/2,screen->h/2,0,0);
btn_quit.Render(menu,screen);
if(btn_quit.IsIn(mouseX,mouseY)){
Button btn_settings(screen->w/2,screen->h/2+70,0,0);
btn_settings.Render(menu,screen);
}
SDL_Quit
btn_quit
工作正常,但是当我将鼠标悬停在按钮上时,我似乎无法让 case 语句之后的 if 语句返回 true 。任何想法为什么会这样?