Sign in to follow this  
SabinOrochi

Animated Sprites =S

Recommended Posts

Hi! My name is Cristian and Im learning to program videogames reading tutorials and samples in the internet. I can draw sprites without difficult, but I cant understand how to move the frames of the animation with the correct time. I did something like this: public void CaminarIzquierda() { spriteHeroe.PosX -= spriteHeroe.VelocityX; nFrames = 4; frames = new Rectangle[nFrames]; for (int i=0;i<nFrames;i++) { Rectangle rectangulo = new Rectangle(i*32,64,32,64); frames[i] = rectangulo; SourceRectangle = frames[i]; } } But it only draws my character moving X pixels with the last frame of the animation. I doesnt draw others three frames, only the last one. I tried to do this too: public void Update(float segundos) { ultimoUpdate += (float)DXTimer.GetElapsedSeconds(); if (ultimoUpdate >= segundosPorFrame) { currentFrame = (currentFrame == ultimoFrame) ? primerFrame : currentFrame + 1; ultimoUpdate -= segundosPorFrame; } } But it the same... it doesnt work like I want. Please... Correct me and tell me how must I do it plz =3 Thanks for reading and byez =) PD: Sorry if I wrote bad, but my language is the spanish =P and I dont know writing english very well =P

Share this post


Link to post
Share on other sites
When posting code, use the [ source] [/ source] tags. (without the spaces before the 's'.)

So your code is:

public void CaminarIzquierda()
{
spriteHeroe.PosX -= spriteHeroe.VelocityX;
nFrames = 4;
frames = new Rectangle[nFrames];

for (int i=0;i<nFrames;i++)
{
Rectangle rectangulo = new Rectangle(i*32,64,32,64);
frames[i] = rectangulo;
SourceRectangle = frames[i];
}
}


and

public void Update(float segundos)
{
ultimoUpdate += (float)DXTimer.GetElapsedSeconds();

if (ultimoUpdate >= segundosPorFrame)
{
currentFrame = (currentFrame == ultimoFrame) ? primerFrame : currentFrame + 1;
ultimoUpdate -= segundosPorFrame;
}
}



Notice how much more readable the code is?

In reply to your question, take a look here. It uses SDL for drawing, but should give you a good idea for the general algorithm.

Share this post


Link to post
Share on other sites
Thanks for teaching me about the source tag =P

I think that what my code needs is to be more slowly, cause the animation loop works so fast, and just the last frame is drawn.

When this method is called:



public void Update()
{
ultimoUpdate += (float)DXTimer.GetElapsedMilliseconds();

if (ultimoUpdate >= segundosPorFrame)
{
currentFrame = (currentFrame == ultimoFrame) ? primerFrame : currentFrame + 1;

ultimoUpdate -= segundosPorFrame;

}
}



The if always is False, cause GetElapsedMilliseconds is so fast, and I dont know how to do it slow enough.

Share this post


Link to post
Share on other sites
I haven't dealt with animation for a while, so I'm a little rusty.

First off, can you post your code in english? =) That way we can help you easier as it is somewhat difficult to understand what you are currently doing.

Implimenting a FPS lock should help. It appears you have tried this. Try something like


DWORD start_time = GetTickCount();
//do your work here

while (GetTickCount() - start_time)< var_for_how_long_each_frame_is);


Share this post


Link to post
Share on other sites
Well... I followed a lot of tutorials but I never Understood how fps works...

I have this, but I dont know how it works...

class DXTimer
{
// Here are the variables and other methods =3

public static long TicksPerSecond
{
get
{
return lTicksPerSecond;
}
}
public static double GetElapsedMilliseconds()
{
// Check if initialized
if (!bInitialized)
{
throw new Exception("Timer not initialized!");
}

// Get current number of ticks
QueryPerformanceCounter(ref lCurrentTime);

// Calculate number of milliseconds since last call
dElapsedMilliseconds = ((double)(lCurrentTime - lLastTime) / (double)lTicksPerSecond) * 1000.0;

// Store current number of ticks for next call
lLastTime = lCurrentTime;

// Return milliseconds
return dElapsedMilliseconds;
}
}


And this other class:

public class Fps
{
public void Update(float ElapsedMilliseconds)
{
// add elapsed time and count frames
fAccumulatedTime += ElapsedMilliseconds;
iFrames++;

// calculate fps every second
if (fAccumulatedTime > 1000.0f)
{
fFps = (float)iFrames / fAccumulatedTime * 1000.0f;

iFrames = 0;
fAccumulatedTime = 0.0f;
}
}
}


I really dont understand these classes =(

Please tell me what methods I must use and how it works =3

And thanks for replying me =)

Share this post


Link to post
Share on other sites
Uh...what language are you using? I assumed C++ but that

get
{
return foo;
}

isn't c++ afaik...

FPS stands for frames per second.

I don't have time to really explain atm, but basically you just need to see if you sprite requires its animation updated, check to see how much time its been since the last update (use GetTickCount()), if its been longer than time x, update to the next frame, otherwise, continue on with your main game loop.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this