Jump to content

  • Log In with Google      Sign In   
  • Create Account

[C++] Sprite disappearing


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Applegenie   Members   -  Reputation: 109

Like
0Likes
Like

Posted 28 March 2012 - 05:12 AM

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 Posted Image 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() );
		}
	}


Sponsor:

#2 thestien   Members   -  Reputation: 102

Like
0Likes
Like

Posted 28 March 2012 - 06:29 AM

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

#3 Endurion   Crossbones+   -  Reputation: 3575

Like
0Likes
Like

Posted 28 March 2012 - 08:04 AM

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.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#4 Applegenie   Members   -  Reputation: 109

Like
0Likes
Like

Posted 29 March 2012 - 02:06 AM

Thanks for the help guys. Like Endurion said, the problem was related to loading the image. Got it fixed now Posted Image Thanks again




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS