Game crashes when character goes out of screen

Started by
5 comments, last by Cosmic314 10 years, 6 months ago

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?

Advertisement
Do you need to turn gravity off?

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

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

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.

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

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?

This topic is closed to new replies.

Advertisement