Win32 GetAsyncKeyState problem >=\ (big long problem,helper will get credit in game)
Some Background Info:
Ok, I've made a small mario game, and I've got a weeeee bit of a problem. At the main screen, mario is pointing at one of two selections: Play, or Quit. What quit does is obvious, but when mario points at play and the user presses the return key, I want the play sprite to move across the screen until it is off screen and have a Load Game button slide back to the right so the user can pick which load game (again, mario points to the one the user selects.)
Now, for my problem:
What I did to animate the Play button is at every Game Cycle (i made my game to run at 50fps) the program checks if a variable, iAnimating is equal to 1 or 2 (1 means the play button should move to the left, 2 means the load button should move to the right). Then it calls the AnimatePlayButton() function that i created. This just sets the playbuttons x value to a little less than before so that it moves a few pixels left across the screen at every Game Cycle while iAnimating is equal to 1. Then when the button reaches a certain x value (-77 to be exact), iAnimate = 2 and the LoadGame button starts moving to the right. It is a very weird problem, but for some reason the objects stop moving unless I keep the return key pressed, or any other key for that matter. (eg, i press return and release it, then the button moves a little. to make the animation keep playing i have to keep any key pressed). Here are some code snippets for you so you know what I mean:
this function is called every cycle:
void HandleKeys(HWND hWnd)
if (GetAsyncKeyState(VK_ESCAPE) < 0)
SendMessage(hWnd, WM_DESTROY, 0, 0);
if (iLevel == 2)
if (GetAsyncKeyState(VK_RETURN) < 0)
if (SelectMode == 1)
iAnimating = 1;
iStartScreenMode = -1;
and so on so forth, the rest of the function is irrelevant. the iStartScreenMode is set to -1 so that mario cant move his arms during the animation. SelectMode just checks if mario is pointing at the Play button and not the Quit button when return is pressed.
this is the game cycle that calls the animation:
void GameCycle(HWND hWnd)
if(iAnimating != 0)
and heres the AnimatePlayButton function:
if (iAnimating == 1)
if (PlayQuit.x > -77)
PlayQuit.x = PlayQuit.x - 10;
if (PlayQuit.x <= -77)
SelectMode = 3;
Load12.x = -210;
iAnimating = 2;
if (iAnimating == 2)
if (Load12.x < 108)
Load12.x = Load12.x + 10;
if (Load12.x >= 108)
iStartScreenMode = 1;
iAnimating = 0;
I know this problem is extremely confusing putting it lightly, but if there are any programmers who see my mistake PLEASE post here, I'm completely stumped :( (I bet it's just a stupid mistake but I can't spot it)
If you would like the full code for the game beginning, and are willing to take on the challenge email me at:
and I will send you my code so far..but just a note, I'm not going to send you the bmps I used because they are far too large.
THANKS A MILLION :) :) whoever helps will definitly get credit.
Look at every instance of ianimating. I am willing to bet you set it to zero or something like that incorrectly at one place or another.
lol nope, i searched all my code and the only place i set it to 0 is at the very beginning
EDIT NVM i got so tired of trying to figure out what was wrong I just used WM_TIMER instead of GameCycle() to time the animation and it worked...though i have a funny feeling something was looping when it wasn't supposed to somewhere in the wndproc.
Last edited by f4cepl4nt; 03-08-2005 at 09:35 PM.
I suggest you try a thread instead, actually. The timers are very limited (you can only have a couple of them -- they are a scarce system resource) and they are not fast (the fastest timee event is relatively slow on a modern computer). It probably does not matter for a simple image animation, but inside the actual game a thread could really kick you up a few notches.
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL