Jump to content
  • Advertisement
Sign in to follow this  
Prog101

making my sprite animate

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

i am trying to get my sprite to loop through 4 frames on the bmp to animate it when i press a cursur key. i'm using DirectX 9. I call the GetGameKeys();in the gameloop and it increments my sprite but increm always looks like it is at 4, i dont know if it is calling it too fast in the loop or what? HRESULT GetGameKeys() { if (KEYDOWN(KeyboardState, DIK_RIGHT)) { for (increm = 0; increm < 4; increm++) { player.top = 0; player.bottom = 32; player.right = 32 * increm; player.left = player.right - 32; g_Player.SetSourceRect(player); if (increm == 4) { increm = 0; } } return S_OK; }

Share this post


Link to post
Share on other sites
Advertisement
The player animates far to fast. i know its because its catching each frame, but what is another way to do it? just to slow it down and get a smooth animation

//****************************************Drawing routines for the player
g_Player.Render(g_pDevice, g_pBackSurface);
//Animate the player running right
PlayerAnimState pPlayerAnimstate = anim_getState();

if (pPlayerAnimstate == RIGHT)
{
player.top = 0;
player.bottom = PlayerHeight;
player.right = PlayerWidth * currentFrame;
player.left = player.right - PlayerWidth;
g_Player.SetSourceRect(player);

//update the current frame
currentFrame++;
if (currentFrame == MaxFrames)
{
currentFrame = 0;
}
}

Share this post


Link to post
Share on other sites
You should limit the framerate, at first.
I suggest you have a look at this for instance.
Might be more than 3 minutes of reading, but useful.

Share this post


Link to post
Share on other sites
You can update your position every update cycle, but you need to limit how often you change sprite frames.

One way to do that is to limit it by time, so:

timeNow = GetCurrentTime();
if(timeNow > timeToUpdate)
{
DoUpdateStuff();
timeToUpdate = timeNow + TIME_BETWEEN_UPDATES;
}

The code in this tutorial does this.

http://www.toymaker.info/Games/html/2d_animation.html

Another way would be to monitor the position of the player and 'step' (change frames) after a certain displacement had occured (e.g. change after 6 pixels has been moved). This is harder, in my experience.



Share this post


Link to post
Share on other sites
for (increm = 0; increm < 4; increm++)


When you hit the key, the game will loop through all of the frames of animation.

When you hit the key, increm will go 0, 1, 2, 3 and exit. You can verify this with a printf. It will not advance while you hold down the key; it will just blow through all the frames and return to the first one.

It will not advance the animation frame each game frame; what I would do is something more like this:

if(keydown) {
if(lastUpdate > update time) then
frame++
if frame >= 4 then
frame = 0
}

Share this post


Link to post
Share on other sites

for (increm = 0; increm < 4; increm++)
{
// ...

if (increm == 4)
{
increm = 0;
}
}


Just pointing something out: increm will never get to 4 in this loop, so, you won't see it coming back to 0.

An alternative way to Ravuya's is:


if animation is not stopped and (current_time > update_time)
update animation
set update time

if key is pressed
start animation
else if key is released
stop animation

Share this post


Link to post
Share on other sites
Solved it cheers guys
here is what i did

//Declare
LARGE_INTEGER timeStart;
LARGE_INTEGER timeEnd;
LARGE_INTEGER timerFreq;
float anim_rate = 0.0f;
float fLastAnimTime = 0.0f;
float fTimerFrequency = 0.0f;


// Initialize the timer frequency in winmain
::QueryPerformanceFrequency(&timerFreq);
fTimerFrequency = 1.0 / timerFreq.QuadPart;

// set the frame data in init function
g_Player.curFrame = 0;
g_Player.numFrames = 4;

//in render before draw:
//query the counter to catch the start time
::QueryPerformanceCounter(&timeStart);
//Draw stuff
::QueryPerformanceCounter( &timeEnd );
frameAnimTime = timeEnd.QuadPart * fTimerFrequency;

if (anim_getState()==RIGHT)
{
// make the sprite animation loop through the frames
if ( frameAnimTime - fLastAnimTime > 0.5f ) // 1/2 second
{
if ( ++g_Player.curFrame == g_Player.numFrames )
g_Player.curFrame = 0;

fLastAnimTime = frameAnimTime;
}
// set the source rect to the correct frame position
player.top = 0;
player.bottom = PlayerHeight;
player.left = g_Player.curFrame * PlayerWidth ;
player.right = player.left + PlayerWidth;
g_Player.SetSourceRect(player);
//End draw stuff
::QueryPerformanceCounter(&timeEnd);
anim_rate = ( (float)timeEnd.QuadPart - (float)timeStart.QuadPart ) / timerFreq.QuadPart;

//then we can use anim_rate for a smooth movement for the player
//move player
g_Player.m_fTranslateX += g_Player.moveX * anim_rate;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!