I have a program that displays 5 explosions in a line at the same time. I would like to know how to get each explosion to animate at different rates. Here is the code that actually animates the sprites:
//=========================================================================
//
// Helper functions
//
//========================================================================
void Sprite_Draw_Frame(LPDIRECT3DTEXTURE9 texture, int destx, int desty, int framenum, int framew, int frameh, int columns)
{
D3DXVECTOR3 position( (float)destx, (float)desty, 0);
D3DCOLOR white = D3DCOLOR_XRGB(255, 255, 255);`
RECT rect;
rect.left = (framenum % columns) * framew;
rect.top = (framenum / columns) * frameh;
rect.right = rect.left + framew;
rect.bottom = rect.top + frameh;
spriteobj->Draw(texture, &rect, NULL, &position, white);
}
void Sprite_Animate(int &frame, int startframe, int endframe, int direction, int &starttime, int delay)
{
if((int)GetTickCount() > starttime + delay)
{
starttime = GetTickCount();
frame += direction;
if(frame > endframe) frame = startframe;
if(frame < startframe) frame = endframe;
}
}
//============================================================================
//
// Function calls
//
//==============================================================================
Tried to use random numbers for the delay variable in Sprite_Animate
so that the frames would be delayed at different rates. However, the explosions continued to animate in sync.
The Sprite_Animate
function just updates the global variables frame
and starttime
to continue drawing each new frame with the Sprite_Draw_Frame
function.
//animate and draw the sprite
for(int i = 0; i < 5; i++)
{
Sprite_Animate(frame, 0, 29, 1, starttime, rand() %100);
Sprite_Draw_Frame(explosion, (i * 100), 100, frame, 128, 128, 6);
}
There is a simple solution to this, and if anyone wants the full code I can send it to you by request.
SO i figured it out. The reason why they were all in sync even with random delay
values is because they were all sharing the same global variables frame
and starttime
.
So instead, creating a structure of EXPLOSION
with the member variables frame
and starttime
and than using a vector to contain them all. After it just had to be iterated through.
The new code is here in case someone else comes along this problem.
//The structue to contain each explosions own frame and starttime variables
struct EXPLOSION
{
int frame;
int starttime;
};
//============================================================================
//
// Function calls
//
//==============================================================================
//animate and draw the sprite
for(int i = 0; i < 5; i++)
{
Sprite_Animate(explosions[i].frame, 0, 29, 1, explosions[i].starttime, rand() % 100);
Sprite_Draw_Frame(explosion, i * 100, 100, explosions[i].frame, 128, 128, 6);
}