Archived

This topic is now archived and is closed to further replies.

Koobazaur

Trying to make a movable pad - weird bug!

Recommended Posts

Well, I''ve been trying to make a movable pad - if you hold up, it goes up, if you hold down, it goes down... Here is basically the code which is called as often as possible (but max 1000 times a second)...
bool MovePads(MSG msg) //I pass the message I get through PeekMessage

{
 static KTimer MoveTimer; //KTimer is my own structure. It''s a ...timer.... wow...

 static float fTP; 
 static WPARAM LastParam=0;

 if(!Pads[0].Control) //this basically checks if the pad is controlled by player, skip this!

 	{
    fTP = MoveTimer.HowMuch(0); //this gets how much time (in seconds) passed since I last called the MoveTimer.Reset() method


    if(fTP < 0.05f) //if that time is less than 0.05 second

    	{
       if(msg.message==WM_KEYDOWN) //AND if the user pressed a key

       	{
       	 if(LastParam!=msg.wParam)  //if our "last" key presssed doesn''t equal this one, this means the user is just starting the "holding key" sequence...

				fTP=0.0f; //and so, lets set this to 0.


          LastParam = msg.wParam; //so we set our last param as the param we got when we pressed a key...

	       MoveTimer.Reset(); //reset timer

         }
      }else{ //if more than 0.05 seconds passed...

       LastParam = 0; //set last param to 0 because that means the player is no longer holding a key :(

       MoveTimer.Reset(); //reset the timer please...

      }

       switch(LastParam) //now our switch which checks the LastParam

       	{ //basically the amount the pad should move is the time passed (fTP) times its speed. Pad[0].Y is the Y coord of the pad

          case VK_UP:   Pads[0].Y -= fTP * PAD_SPEED; break;
          case VK_DOWN: Pads[0].Y += fTP * PAD_SPEED; break;
         }
	
   }
}
[source]

Basically here''s how it works:
User presses a key
Program remembers that and counts 0.05 seconds, moving pad at the same time. After that time passes, the program forgets that user pressed a key and stops moving pad.
IF the user presses this key again within 0.05, the "counting" is reset...
so as long as the key is pressed with intervals no longer than 0.05 seconds, it counts as "holding the key"...

My problem is that when I single-press a key, the pad kinda "jumps" into given direction. It''s not very smooth and the leap is way too big (it moves too fast). And I can''t figure out why!!! 
So when I hold a key it does this:
leap up a couple of pixels
pause for like 0.5 second
go up normally, just like I intended it to...

I really can''t figure out this one, can anyone help?



Share this post


Link to post
Share on other sites
Well, I discovered that whenever the "stop" after pressing a key occours, the

}else{ //if more than 0.05 seconds passed...
LastParam = 0; //set last param to 0 because that means the player is no longer holding a key
MoveTimer.Reset(); //reset the timer please...
}


Gets executed and that's the reason my pad stops...
If I change the


if(fTP < 0.05f)


to


if(fTP < 0.2f)


(setting the max delay between key presses which count as "holding the key down") it will work ok then...

But why when I first press a key the time it takes to re-run this function is longer (therefore my if statement returns false and executes the "Else" part) but then it "speeds" up and the time is short enough to make he if TRUE every time...

btw, my main loop looks something like this:

while(1)
{
if(PeekMessage(&msg,0,0,0, PM_REMOVE))
{
if(msg.message==WM_QUIT)
break;

TranslateMessage(&msg);
DispatchMessage(&msg);
}else
{//Here's where the magic starts!

if(BasicTimer.Passed(1)) //this ensures that we don't exec this loop more than 1000 times a second, otherwise the program will work choppy

{
DoGame(msg);//Do Game

SBuf.Display(); //lets display the back buffer!

msg.message=0; //we set the message to zero every time because otherwise one message would repeat itself over and over if there was no new one... leading to glitches

}
}
}



Help would be extremally appriciated...

[edited by - Koobazaur on March 21, 2004 2:58:17 AM]

Share this post


Link to post
Share on other sites
If you could zip all your project files and put them on a place where i can download and compile them, i will try to help you. i find it much harder to work with parts of code that can''t compile and test.

Share this post


Link to post
Share on other sites