Public Group

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.

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 on other sites
Do you need to turn gravity off?

Share on other sites

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

Share on other sites

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.

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 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 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?

• 21
• 12
• 9
• 17
• 13