Moving a box with the mouse


i try to make a program that let me move a box using mouse. But when i use the condition if(m_event.button.type == SDL_MOUSEBUTTONDOWN && m_event.button.button == SDL_BUTTON_LEFT), it works fine but if i move the mouse condition is not longer true, why ?

Hello and welcome to the forum!

I’m afraid it’s very hard helping you with the problem, without looking at your code. Please edit your post, or answer to this post, and paste the code in. Remember to surround your code in code tags so it’s easy to read it. To surround your code in code tags, highlight your code and press the </> button in the text formatting-menu.

If you did it properly, your code formatting should look like this:

int main()


Ok here we go,

my code looks like that :

if(m_event.button.type == SDL_MOUSEBUTTONDOWN && m_event.button.button == SDL_BUTTON_LEFT)
//if cursor inside box  then move the box with mouse information

Like said, when i keep mouse unmoved and leftclick condition is true, if i do same but moving the mouse too, condition turn false. So why this condition is false when i move the mouse ?

It’s an event (that is, a transition between states), not a state. It fires when the mouse button is clicked (you get an SDL_MOUSEBUTTONUP event when it is released) but simply moving the mouse doesn’t result in either. You should probably be responding to SDL_MOUSEMOTION events, or alternatively you can check the state of the mouse directly with SDL_GetMouseState().

Ok, but then why condition continues to be true if i keep my finger on the leftclick, when i remove finger it comes false.

Luck! Possibly the data is held in statically-allocated memory that just happens to retain its value for a while after the event has been serviced. Really it’s irrelevant; you shouldn’t be doing that whether or not it appears to work (some of the time). If you break the rules it doesn’t mean it’s guaranteed not to work, just that it isn’t guaranteed to work. :grinning: