I have come across a situation I have not encountered before using the IF control structure, with a further nested IF. Normally the nested IF would break down what I am checking in more fine grained detail, but in this instance it actually conflicts with the outer conditional - I am checking for a mouse click.
Here is the code -
if (mouseState.LeftButton == ButtonState.Pressed)
{
if (mouseState.LeftButton == ButtonState.Released)
{
//move sprite to clicked position
spritePos.X = mouseState.X;
spritePos.Y = mouseState.Y;
}
}
I have a couple of questions. I was taught that the conditional argument is only checked once, if it evaluates as true then the following statements are executed. Is this true, or is it actually checked before executing every statement within the block? - as the latter would obviously cause conflict with my nested if.
If this is true, then why does my button click method not work? The sprite never moves as though the mouse release never executes. Is it because the delay between performing a check on both conditionals is so small, that I can not physically have released the button within that time? Can I be slightly hackish about this and overcome it with a time delay which might be large enough to allow the release of the mouse button by the time of checking the nested conditional?
Methods for mouse click actions that I have seen, use properties of the mouse from the previous frame, and compare to the new frame to check for a mouse click. But this method must introduce a delay of at least one frame to actually process the action. This might not be noticeable with high framerates, but just for arguments sake say I had a game rendering 20fps. UpdateInput() would be called before rendering any frame, positions should update and then the frame should render last.
If the full mouse click operation could operate within less than 1/20th of a second, wouldn't it be preferrable to process the action at once rather than store a variable upon the initial press, and compare states in the next frame? This would surely be better than introducing further delay on input.
I'm sure this must have been considered by others before though, and perhaps I'm not thinking correctly about the time delay operation actually pausing execution of the code. Can time delays pause processor execution of code, or only the result of those executions such as rendering and accepting input? They are not something which I have any experience in using, and I may be confusing how a delay would work.
Thank you, Andrew.