Jump to content
  • Advertisement
Sign in to follow this  
ISDCaptain01

Game crashes when character goes out of screen

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

Im making a sidescroller. When the character drops into a bottomless pit, I want to display a gameover screen, but instead the game crashes.

Im using MappyAL and Allegro 4.2 to make this game. Here is the key algorithm:

//Keep the character from crashing the game if he goes out of screen 750*20
		if(player->x < 0) player->x = 0;
		if(player->y < 0) player->y = 0;
		if((player->x + player->width) > (mapwidth * mapblockwidth))
			player->x = (mapwidth * mapblockwidth) - player->width;
		if((player->y + player->height) > (mapheight * mapblockheight))
		{
			player->y = (mapheight * mapblockheight) - player->height;
			blit(gameover, buffer, 0, 0, 0, 0, WIDTH, HEIGHT);
		}

The 750 * 20 basically means how many blocks there are horizontally and vertically respectively.  Each block is 32 * 32 pixels. So mapheight(20) * mapblockheight(32) gives me my vertical level dimensions. If the player crosses this limit, than blit the gameover screen. But that doesn't happen. Can someone point me in the right direction?

Share this post


Link to post
Share on other sites
Advertisement

Hi there.

Does it have a grid of cells may be it does a memory look up with the X, Y and when there out of range so is the lookup address.

rolleyes.gif

Share this post


Link to post
Share on other sites

Haha, I got absolutely the same problem. For me the case was that the calculation of the player's position led to an array of out bounds error that caused a crash for me as well. I sort of fixed it, but Super Mario Bros from 1985 did it much better than I did.

 

However I am also now getting some edge-case bugs that I am not sure how to resolve. So yeah, I would also love to see how to fix this better.

Share this post


Link to post
Share on other sites

Does it crash when the player falls off screen or when Allegro attempts to draw gameover to buffer?

 

crash when the player falls off the screen

Share this post


Link to post
Share on other sites

I assume your lowest point is y = 0 as you clamp to this value if the character's coordinates ever go less than 0.

 

mapheight * mapblockehight = 640.  Based on your y=0 position clamp, 640 represents a place "off" the screen.  Pixel 639 is 'on' the screen.  With just the '>' symbol you're skipping the very first line of pixels off the screen.  This means, possibly, that the very top of your character's pixel row will be rendered offscreen, depending on how you define player->y and player->height.  If player->y represents the first vertical pixel and player->height represents the number of pixels in height your player is, you'll have an "off by one" issue in the above checking code.

 

In your position update code, do you ever clamp the y value?

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!