SFML community forums
Help => Window => Topic started by: Chuckleluck on November 23, 2011, 04:56:12 am
-
Hello,
I'm working on a program and I need my character's feet to move back and forth while he's walking. I've got everything working with sf::Clock. Here's a snippet of my code:
void MovePlayer(int x, int y, int Player)
{
switch(Player)
{
case 1:
g_sPlayerHead.Move(x, y);
g_sPlayerBody.Move(x, y);
g_sPlayerFeet.Move(x, y);
g_sPlayerArms.Move(x, y);
int timer = FootImgClock.GetElapsedTime();
switch(timer)
{
case 0:
g_sPlayerFeet.SetSubRect(g_rcFeetMveA);
break;
case 1:
g_sPlayerFeet.SetSubRect(g_rcFeetMveB);
break;
case 2:
FootImgClock.Reset();
break;
}
default:
break;
}
}
The feet move too slow for my liking, once every second or so. How do I fix this? Is there a version of sf::Clock that measures in miliseconds?
-
The measured time is a float representing the ammount of seconds that has passed. By putting it into an integer, you are cutting the smaller parts and taking only the integer part. If you want milliseconds, just multiply the float by 1000 before truncationg it into an integer.
-
The measured time is a float representing the ammount of seconds that has passed. By putting it into an integer, you are cutting the smaller parts and taking only the integer part. If you want milliseconds, just multiply the float by 1000 before truncationg it into an integer.
In SFML2 GetElapsedTime is a Uint32 measuring time in milliseconds. I think the X and Y should be floats not ints.
If you want an accurate timer, do not reset it, instead calculate the difference
last_frame = FootImgClock.GetElapsedTime();
while (running)
{
frame_time = FootImgClock.GetElapsedTime() - last_frame;
last_frame = FootImgClock.GetElapsedTime();
Player.Move(frame_time, 0);
}
-
Can someone show me what I should do to modify the code I posted? I don't have much experience with floats. I'm also using SFML 1.6, not 2.
-
Change
int timer = FootImgClock.GetElapsedTime();
to
int timer = FootImgClock.GetElapsedTime() * 2;
and you will have 2 animation frames per second, instead of one.
But I think you should study more of the basics before attempting to do something.
-
Nevermind. I figured it out :D
For anyone who's interested, this is how I edited the code:
void MovePlayer(int x, int y, int Player)
{
switch(Player)
{
case 1:
g_sPlayerHead.Move(x, y);
g_sPlayerBody.Move(x, y);
g_sPlayerFeet.Move(x, y);
g_sPlayerArms.Move(x, y);
float ftimer = FootImgClock.GetElapsedTime();
if(ftimer <= .3)
g_sPlayerFeet.SetSubRect(g_rcFeetMveA);
else if(ftimer >= .4 && ftimer <= .7)
g_sPlayerFeet.SetSubRect(g_rcFeetMveB);
else if(ftimer >= .9)
FootImgClock.Reset();
default:
break;
}
}
-
There's nothing much to floats (that's sort of a lie). For your purposes, think of a float as something that can store a decimal number to about 8 significant figures. A double can store about 16 significant figures.
-
There's nothing much to floats (that's sort of a lie). For your purposes, think of a float as something that can store a decimal number to about 8 significant figures. A double can store about 16 significant figures.
That double precision is worthless when you're losing the fractions of a pixel each frame of not using floats.