# GetTickCount()

This topic is 4040 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, Sorry for the embarrassing questio coming up but I have a problem with jumping in a game. It's just a basic reconstruction of the old classic Chucky Egg. By using GetTickCount() I have managed to control the character's left and right smoothly, but, when I press the jump key it jumps far too fast. The test for the jump key is in the same place as the test for the left and right keys and they seem to be fine. Why is the jumping action acting like theres no delay at all?? Please can someone help and keep in mind that I'm very new to gaming in c++. styfir.

##### Share on other sites
You need to provide some more details. Most of us aren't psychic and hence fail to magically know how your code works.

##### Share on other sites
When you press Jump, there really exist delay. But they're still too short. Game will continually get keypress message.

You can add an int to lengthen this delay.

//game initint lendelay=0;//game main loop//key press check partif (keypressed(jump)&&(lendelay==0)){//do something herelendelay = 10; //maybe some larger number}//at the end of game loopif (lendelay>0) lendelay--;

##### Share on other sites
ok heres the code then

#include "Globals.h"

void Main_Loop()
{

HDC hdc;
hdc = GetDC(GameHandle);

if (KEYDOWN(VK_ESCAPE))
{
SendMessage(GameHandle, WM_CLOSE, 0,0);
}

chuckyendtime=(GetTickCount()-chuckystarttime);
if ((int)chuckyendtime>chuckyspeed)
{
if (KEYDOWN(VK_SPACE) && (lenDelay==0))
{
for(int i=1; i<60; i++)
{
Clear_Chucky();
py=py-gravity;
Draw_Chucky();

//gravity+=1;
}
gravity=1;
lenDelay=100;
}

while(GetPixel(hdc,px+3,py+35)==RGB(0,0,0))
{
Clear_Chucky();
py=py+gravity;
Draw_Chucky();

//gravity+=1;

}

if (KEYDOWN(VK_RIGHT))
{
if (px<757)
{
Clear_Chucky();
px+=3;

// when moving right check to see if theres a ladder //
{
contact=true;
}
if(contact)
{
}
Draw_Chucky();
}
}

if (KEYDOWN(VK_LEFT))
{
if (px>3)
{
Clear_Chucky();
px-=3;

// when moving left check to see if theres a ladder //
{
contact=true;
}
if(contact)
{
}
Draw_Chucky();
}
}

}
chuckystarttime=GetTickCount();
ReleaseDC(GameHandle,hdc);

}

See if you can find the problem I'd be really grateful Ta

##### Share on other sites
Quote:
 lendelay=100;

maybe it's just too large. the number here, take 100 as example, means you multiply delay by 100.

##### Share on other sites
Ive tried smaller numbers like 50 and 25 but still, no effect on the speed - I just dont know...

thanks anyway,

styfir.

BTW how do you post code and have it all nice and indented?

##### Share on other sites
[ source ] [ /source ] tags.

##### Share on other sites
use [souce] and [/source] tag

##### Share on other sites
Thank you

any more thoughts on the problem at hand??

styfir.

##### Share on other sites
granted I might have missed something, but lenDelay doesn't do anything as far as I can see in the code you posted. It's tested for zero in an if, and if it is, it's set to 100. Also, you have a for loop that does nothing but run through the code in the loop 60 times, as fast as it can. No timing is involved in the loop, so that's why it looks like GetTickCount has no effect on it, 'cause it doesn't. Again, I might have missed something, but that's what I can gather.

If you've modified the code at all (and even if you haven't) post it up properly so that the indentation/formatting is seen, it's a lot easier to see an error that way, for us and for you.

##### Share on other sites
if (KEYDOWN(VK_SPACE) && (lenDelay==0)){    for(int i=1; i<60; i++)  // you basically redraw 59 times here without any    {                        // any delay whatsoever.        Clear_Chucky();        py=py-gravity;         Draw_Chucky();        //gravity+=1;    }    gravity=1;    lenDelay=100;}while(GetPixel(hdc,px+3,py+35)==RGB(0,0,0)) // at this point, you move your{                                           // character back, again without    Clear_Chucky();                         // any delay    py=py+gravity;     Draw_Chucky();    //gravity+=1;}

HTH,
Pat.

##### Share on other sites
Ok now that I know how to post code I'll give it a go..............

[SOURCE]#include "Globals.h"void Main_Loop(){   		HDC hdc;    hdc = GetDC(GameHandle);	if (KEYDOWN(VK_ESCAPE))	{			    SendMessage(GameHandle, WM_CLOSE, 0,0);			}	chuckyendtime=(GetTickCount()-chuckystarttime);	if ((int)chuckyendtime>chuckyspeed)	{		if (KEYDOWN(VK_SPACE) && (lenDelay==0))		{			for(int i=1; i<60; i++)			{				Sleep(1);				Clear_Chucky();				py=py-gravity;								Draw_Chucky();								//gravity+=1;			}			gravity=1;			lenDelay=10000;		}				while(GetPixel(hdc,px+3,py+35)==RGB(0,0,0))		{			Sleep(1);			Clear_Chucky();			                 //SetPixel(hdc,200,300,RGB(255,255,255));			py=py+gravity;						Draw_Chucky();						//gravity+=1;				}		if (KEYDOWN(VK_RIGHT))		{			if (px<757)			{				if (KEYDOWN(VK_SPACE) && (lenDelay==0))				{				for(int i=1; i<60; i++)				{					Sleep(1);					Clear_Chucky();					py=py-gravity;										Draw_Chucky();										//gravity+=1;				}				gravity=1;				lenDelay=10000;			}				Clear_Chucky();				px+=3;								// when moving right check to see if theres a ladder //				if(GetPixel(hdc,px+27,py)==RGB(60,60,60))  // ladder color				{						contact=true;				}				if(contact)				{					Redraw_Ladder();    // go and redraw corresponding ladder				}				Draw_Chucky();			}		}				if (KEYDOWN(VK_LEFT))		{			if (px>3)			{				Clear_Chucky();				px-=3;				// when moving left check to see if theres a ladder //				if(GetPixel(hdc,px-27,py)==RGB(60,60,60))  // ladder color				{						contact=true;				}				if(contact)				{					Redraw_Ladder();    // go and redraw corresponding ladder				}				Draw_Chucky();			}		}		if (KEYDOWN(VK_LEFT) && (KEYDOWN(VK_SPACE)))		{			SetPixel(hdc,200,300,RGB(0,0,255));		}	}	chuckystarttime=GetTickCount();	ReleaseDC(GameHandle,hdc);	}[/SOURCE]

hope thats better - I hope!!

styfir

##### Share on other sites
Quote:
 Original post by darookie *** Source Snippet Removed ***HTH,Pat.

So if I take out the while loop and the for loop it should be ok??

styfir.

##### Share on other sites
As you can see I have added sleep() commands in there to slow down the jumping action.

styfir.

##### Share on other sites
What you want is a state-based system and a fixed-time update interval. That might sound more complicated than it really is.
Basically what you want to do is this (pseudo-code!):
if (GetTickCount() - lastUpdateTick >= UpdateTickCount) {    // update last ticks - if the code below takes longer than "UpdateTickCount", the game will just update/render as fast as the PC can    lastUpdateTickCount = GetTickCount();    int newX = playerX;    int newY = playerY;    // handle horizontal movement    if (IsKeyDown(KeyMoveLeft)) {         newX -= PixelsPerTick;    }    else if (IsKeyDown(KeyMoveRight)) {         newX += PixelsPerTick;    }    // handle jumping    if (IsKeyDown(KeyJump)) {         newY += JumpHeightPerTick;    }    // handle gravity    else if (!IsPlayerAtBottom(newX, newY)) {         newY -= Gravity;                // apply some gravity         if (newY < GetBottomAt(newX)) { // assuming GetBottomAt(x) returns the bottom y position at horizontal offset x             newY = GetBottomAt(newX);           }    }    // draw the player    ClearPlayerAt(playerX, playerX);    playerX = newX;    playerY = newY;    DrawPlayerAt(playerX, playerX);        // ... draw other stuff}

Hope you get the idea,
Pat.

##### Share on other sites
first of all, a Sleep(1) will do absolutely nothing to slow that down since it's 1/1000th of a second. What you want to do is probably something along the lines of the following: if in one frame, you test the keyboard and someone pushes the jump key, tell your character to "jump." The character's height increases for a set number of frames (since if I'm not mistaken you're limiting the frame rate) and then decreases for a set number of frames. Keep in mind that you don't want to do a loop in the frame as it's all done in one frame, what you want to do is make a change to the height in each frame or maybe even every second or third frame so that the jump animation is slow enough and smooth enough. Does that make sense? Let me know if it's confusing and I'll try again.

##### Share on other sites
Quote:
 Original post by gamezofirst of all, a Sleep(1) will do absolutely nothing to slow that down since it's 1/1000th of a second. What you want to do is probably something along the lines of the following: if in one frame, you test the keyboard and someone pushes the jump key, tell your character to "jump." The character's height increases for a set number of frames (since if I'm not mistaken you're limiting the frame rate) and then decreases for a set number of frames. Keep in mind that you don't want to do a loop in the frame as it's all done in one frame, what you want to do is make a change to the height in each frame or maybe even every second or third frame so that the jump animation is slow enough and smooth enough. Does that make sense? Let me know if it's confusing and I'll try again.

yes that helps alot thanks - I'll post back with results!

styfir.

##### Share on other sites
Quote:
 Original post by darookieWhat you want is a state-based system and a fixed-time update interval. That might sound more complicated than it really is.Basically what you want to do is this (pseudo-code!):*** Source Snippet Removed ***Hope you get the idea,Pat.

I think so, Pat.
Will implement my code and see what happens.

styfir

##### Share on other sites
OK changed the code slightly to this, but still not able to jump, anyone know why?

chuckyendtime=(GetTickCount()-chuckystarttime);	if ((int)chuckyendtime>chuckyspeed)	{		chuckystarttime=GetTickCount();		if (KEYDOWN(VK_SPACE) && jump==true)		{			Clear_Chucky();			py=py-gravity;						Draw_Chucky();		}		// check beneath character feet for 'ground'		// if not then fall		if(GetPixel(hdc,px+3,py+35)==RGB(0,0,0))		{			jump=false;			Clear_Chucky();			py+=1;			Draw_Chucky();		}		else		{			jump=true;		}

##### Share on other sites
what you want to do is set "jump" to true IF space is down AND you're NOT jumping already. then, later in the code, see if you're "jumping", if you are, change the height of the character. Once he's gotten to the high-point of the jump, start moving down instead of up. Once you get to the floor, stop "jumping" (i.e. set jump to false) so that you can jump again.

##### Share on other sites
Quote:
 Original post by gamezowhat you want to do is set "jump" to true IF space is down AND you're NOT jumping already. then, later in the code, see if you're "jumping", if you are, change the height of the character. Once he's gotten to the high-point of the jump, start moving down instead of up. Once you get to the floor, stop "jumping" (i.e. set jump to false) so that you can jump again.

That makes perfect sense when you say it like that - thankyou!

changing code.................

styfir.

##### Share on other sites
right then I have tackled it like you said (I hope) and now hes half jumping - meaning y is being decremted but never incremented - take a look...........

	chuckyendtime=(GetTickCount()-chuckystarttime);	if ((int)chuckyendtime>chuckyspeed)	{		chuckystarttime=GetTickCount();		if (KEYDOWN(VK_SPACE) && !jumping)		{				jumping=true;		}		else		{			jumping=false;		}		if(jumping)		{			Clear_Chucky();			py-=1;			Draw_Chucky();		}		else		{			if(py<470)			{				Clear_Chucky();				py+=1;				Draw_Chucky();				if(GetPixel(hdc,px+3,py+35)==RGB(0,0,0))				{					jumping=false;				}			}		}

##### Share on other sites
Avery BIG thankyou from the newbie to all who helped me out with my jumping routine as I eventually got it working within the GetTickCount() routine!!!!

Heres the code (ignore the canClimb var.)(thats my next task!) for any other newbies to the 'jumping' problem.

	chuckyendtime=(GetTickCount()-chuckystarttime);	if ((int)chuckyendtime>chuckyspeed)	{		chuckystarttime=GetTickCount();		underChucky=GetPixel(hdc,px+4,py+35);		if((underChucky)==RGB(50,50,50)||(underChucky+3)==RGB(50,50,50))		{			canJump=true;		}		if (KEYDOWN(VK_SHIFT) && canJump)		{			SetPixel(hdc,200,300,RGB(0,255,0));			jumping=true;		}		if(jumping)		{			if(py>470)			{				Clear_Chucky();				py-=3;				Draw_Chucky();				canJump=false;			}					else			{				jumping=false;			}				}		if(!jumping && !canJump)		{			Clear_Chucky();			py+=3;			Draw_Chucky();		}

Once again a million thankyous to everyone who gave their time to help me!!!

styfir.