• Advertisement
Sign in to follow this  

[C++] Sprite disappearing

This topic is 2124 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

Could anyone help me with a problem I have? Trying to make a 2d platforming game based on Dracula. Doing this with a team of artists for a school project smile.png The basic concept is that Dracula can suck blood, and the more blood he consumes, the fatter he becomes. He basically has 3 -shapes-. Medium, Fat and Ultra Fat. The problem is that after about 30 seconds, the sprite disappears. I'm guessing this is somehow related to my while loop. I've been following Lazy Foo's tutorials so far.

show function:
void Player::show()
{
//sprites loaded
int rcurrentFrame = -1;
int lcurrentFrame = -1;
int currentClip = 0;
int currentMultiplication = 0;
switch(currentShape)
{
case 1:
cout<<"case 1";
rightFrameAmount = 28;
leftFrameAmount = 28;
CHARACTER_WIDTH = 128;
CHARACTER_HEIGHT = 128;
player_image = load_image("nwalkspritesheet.png");
break;
case 2:
cout<<"case 2";
rightFrameAmount = 32;
leftFrameAmount = 32;
CHARACTER_WIDTH = 128;
CHARACTER_HEIGHT = 132;
player_image = load_image("mwalkspritesheet.png");
break;
case 3:
break;
}
if(player_image == NULL)
{
cout<<"it's nothing";
}
for(int i = 0; i < rightFrameAmount; i++)
{
rcurrentFrame ++;
clipsRight[rcurrentFrame].x = CHARACTER_WIDTH * i;
clipsRight[rcurrentFrame].y = 0;
clipsRight[rcurrentFrame].w = CHARACTER_WIDTH;
clipsRight[rcurrentFrame].h = CHARACTER_HEIGHT;
}
for(int j = 0; j < leftFrameAmount; j++)
{
lcurrentFrame ++;
clipsLeft[lcurrentFrame].x = CHARACTER_WIDTH * j;
clipsLeft[lcurrentFrame].y = CHARACTER_HEIGHT;
clipsLeft[lcurrentFrame].w = CHARACTER_WIDTH;
clipsLeft[lcurrentFrame].h = CHARACTER_HEIGHT;
}
//If left
if( xvelocity < 0 )
{
//left
status = CHARACTER_LEFT;
//Move to the next frame in the animation
frame++;
}
//If right
else if( xvelocity > 0 )
{
//right
status = CHARACTER_RIGHT;
//Move to the next frame in the animation
frame++;
}
//If standing
else
{
//Restart animation
frame = 0;
}
//Loop animation
if( frame >= rightFrameAmount )
{
frame = 0;
}
//Direction
if( status == CHARACTER_RIGHT )
{
apply_surface( character.x, character.y, player_image, screen, &clipsRight[ frame ] );
}
else if( status == CHARACTER_LEFT )
{
apply_surface( character.x, character.y, player_image, screen, &clipsLeft[ frame ] );
}
}


gameloop:
while( quit == false )
{
fps.start();
while( SDL_PollEvent( &event ) )
{
walk.handle_events();
if( event.type == SDL_QUIT )
{
quit = true;
}
}
walk.move();
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
SDL_FillRect( screen, &wall, SDL_MapRGB( screen->format, 0x77, 0x77, 0x77 ) );
walk.show();
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}

Share this post


Link to post
Share on other sites
Advertisement
hi i dont see any reason for him to show for 30 sec and then dissapear but i can see places where you could make your code easier to understand


for(int i = 0; i < rightFrameAmount; i++)
{
rcurrentFrame ++; // use the "i" instead
clipsRight[ i ].x = CHARACTER_WIDTH * i;
clipsRight[ i ].y = 0;
clipsRight[ i ].w = CHARACTER_WIDTH;
clipsRight[ i ].h = CHARACTER_HEIGHT;
}


//Loop animation i didnt see this for him walking the other way
if( frame >= leftFrameAmount )
{
frame = 0;
}

maybe combine both into one block

//If left
if( xvelocity < 0 )
{ //left
status = CHARACTER_LEFT;
//Move to the next frame in the animation
frame++;
if( frame >= leftFrameAmount)
{
frame = 0;
}
}
i dont mean to pick holes in your code but it will make it easier to help you it looks like maybe when moving left could be the problem as it never gets reset to 0. hopefully that will fix it for you

Share this post


Link to post
Share on other sites
It looks like you're loading the image for the player again and again. This of course works for some time, until the memory is exhausted and the image doesn't show up anymore.

Preload the images you intend to use and use those instances.

Share this post


Link to post
Share on other sites
Thanks for the help guys. Like Endurion said, the problem was related to loading the image. Got it fixed now smile.png Thanks again

Share this post


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

  • Advertisement