Crashes When Character Walks

Started by
11 comments, last by LeftyGuitar 9 years, 8 months ago

When I hover over the gPlayer.CurrentFrame, it says gPlayer.CurrentFrame = 0x00000008. I assume this means, its already set the CurrentFrame to 8?


gPlayer.XPos = MAX_WIDTH / 2;
    gPlayer.YPos = MAX_HEIGHT / 2;
    gPlayer.XVel = 0.010f;
    gPlayer.YVel = 0.010f;
    gPlayer.CurrentFrame = 1;
    gPlayer.TotalFrames = 8;
    gPlayer.Width = 48;
    gPlayer.Height = 63;
    gPlayer.LastFrame = gPlayer.CurrentFrame;
    gPlayer.CurrentState = gPlayer.IDLE;
    gPlayer.Facing = gPlayer.DOWN;

gPlayer.LeftImg[0] = IMG_Load("Hero\\w17.png");
	gPlayer.LeftTex[0] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[0]);
	gPlayer.LeftImg[1] = IMG_Load("Hero\\w18.png");
	gPlayer.LeftTex[1] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[1]);
	gPlayer.LeftImg[2] = IMG_Load("Hero\\w19.png");
	gPlayer.LeftTex[2] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[2]);
	gPlayer.LeftImg[3] = IMG_Load("Hero\\w20.png");
	gPlayer.LeftTex[3] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[3]);
	gPlayer.LeftImg[4] = IMG_Load("Hero\\w21.png");
	gPlayer.LeftTex[4] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[4]);
	gPlayer.LeftImg[5] = IMG_Load("Hero\\w22.png");
	gPlayer.LeftTex[5] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[5]);
	gPlayer.LeftImg[6] = IMG_Load("Hero\\w23.png");
	gPlayer.LeftTex[6] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[6]);
	gPlayer.LeftImg[7] = IMG_Load("Hero\\w24.png");
	gPlayer.LeftTex[7] = SDL_CreateTextureFromSurface(GameRend,gPlayer.LeftImg[7]);

for(int i = 0; i <= 8; i++)
	{

		gPlayer.LeftRect[i].x = gPlayer.XPos;
		gPlayer.LeftRect[i].y = gPlayer.YPos;
		gPlayer.LeftRect[i].w = gPlayer.Width;
		gPlayer.LeftRect[i].h = gPlayer.Height;
	}

void RunGame()
{
	while(GameRunning)
	{
		ThisTime = SDL_GetTicks();
		DeltaTime = (float)(ThisTime - LastTime) / MAX_TICKS;
		LastTime = ThisTime;

		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT)
			{
				GameRunning = false;
			}

			if(event.type == SDL_KEYDOWN)
			{
				if(event.key.keysym.sym == SDLK_ESCAPE)
				{
					GameRunning = false;
				}

				if(event.key.keysym.sym == SDLK_UP)
				{
					gPlayer.Facing = gPlayer.UP;
				}
				else if(event.key.keysym.sym == SDLK_DOWN)
				{
					gPlayer.Facing = gPlayer.DOWN;
				}
				else if(event.key.keysym.sym == SDLK_LEFT)
				{
					gPlayer.Facing = gPlayer.LEFT;
					gPlayer.CurrentState = gPlayer.WALK_LEFT;
				}
				else if(event.key.keysym.sym == SDLK_RIGHT)
				{
					gPlayer.Facing = gPlayer.RIGHT;
				}
			}

			if(event.type == SDL_KEYUP)
			{
				if(event.key.keysym.sym == SDLK_UP)
				{
					gPlayer.YVel = 0.0f;
				}
				else if(event.key.keysym.sym == SDLK_DOWN)
				{
					gPlayer.YVel = 0.0f;
				}
				else if(event.key.keysym.sym == SDLK_LEFT)
				{
					gPlayer.CurrentState = gPlayer.IDLE;
					gPlayer.Facing = gPlayer.LEFT;
					gPlayer.XVel = 0.0f;
				}
				else if(event.key.keysym.sym == SDLK_RIGHT)
				{
					gPlayer.XVel = 0.0f;
				}
			}
		}

		UpdateGame();

		DrawGame();
	}
}

void UpdateGame()
{
	if(gPlayer.CurrentState == gPlayer.WALK_LEFT)
	{
		gPlayer.LeftRect[gPlayer.CurrentFrame].x -= gPlayer.XVel * DeltaTime;
	}
}

void DrawGame()
{
	SDL_RenderClear(GameRend);

	SDL_RenderCopy(GameRend,BackdropTex,NULL,&BackdropRect);

	if(gPlayer.Facing == gPlayer.DOWN)
	{
		SDL_RenderCopy(GameRend,gPlayer.IdleFrontTex[gPlayer.CurrentFrame],NULL,&gPlayer.IdleFrontRect[gPlayer.CurrentFrame]);

		gPlayer.CurrentFrame++;
		gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8;
	}

	if(gPlayer.Facing == gPlayer.UP)
	{
		SDL_RenderCopy(GameRend,gPlayer.IdleBackTex[gPlayer.CurrentFrame],NULL,&gPlayer.IdleBackRect[gPlayer.CurrentFrame]);

		gPlayer.CurrentFrame++;
		gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8;
	}

	if(gPlayer.Facing == gPlayer.LEFT)
	{
		SDL_RenderCopy(GameRend,gPlayer.IdleSideTex[gPlayer.CurrentFrame],NULL,&gPlayer.IdleSideRect[gPlayer.CurrentFrame]);

		gPlayer.CurrentFrame++;
		gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 9;
	}

	if(gPlayer.Facing == gPlayer.RIGHT)
	{
		SDL_RenderCopyEx(GameRend,gPlayer.IdleSideTex[gPlayer.CurrentFrame],NULL,&gPlayer.IdleSideRect[gPlayer.CurrentFrame],NULL,NULL,SDL_FLIP_HORIZONTAL);

		gPlayer.CurrentFrame++;
		gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 9;
	}

	if(gPlayer.CurrentState == gPlayer.WALK_LEFT)
	{
		SDL_RenderCopy(GameRend,gPlayer.LeftTex[gPlayer.CurrentFrame],NULL,&gPlayer.LeftRect[gPlayer.CurrentFrame]);

		gPlayer.CurrentFrame++;
		gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8;

		if(gPlayer.CurrentFrame >= 8)
		{
			gPlayer.CurrentFrame = 1;
		}
	}

	SDL_RenderPresent(GameRend);
}

I have posted a rather large chunk of the code. Maybe now I can get closer to solving the problem.

Advertisement

gPlayer.CurrentFrame = 0x00000008 means gPlayer.CurrentFrame is equal to hexadecimal (hex) 8. Hex 8 is equal to 8 in our normal counting system as well. You might want to brush up on hexadecimal numbers if this is new to you. You might also have the option to turn off hex display of values in your IDE, which can make some values easier to read (although some numbers are easier to read in hex in certain contexts).

This means, as I guessed, that you're indexing into the array which has size 8 (and maximum index 7) with the invalid index 8.

The cause is also what I guessed, the appropriate "facing/idle" if checks are always executed, even when the player is moving. This causes the gPlayer.CurrentFrame value to increment (although in a strange matter, which I also touched on in the previous post), up to 8.

After the facing/idle checks, you do the movement anim checks. This part will simply explode if gPlayer.CurrentFrame is 8, and as we see above, it clearly can be.

A simple way to verify (verifying is not fixing) this would be to put the gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8 (or 9) stuff directly before the SDL_RenderCopy calls, forcing the gPlayer.CurrentFrame value to be valid before you use it.

It should no longer crash, but your code should still be fixed properly. I'd recommend trying to find a tutorial or similar for this. The way you're doing animations and drawing now doesn't really work well for a game, in my opinion.

Hello to all my stalkers.

Thanks it dosen't crash. However you do have a valid argument, as this isn't the best way for animation and movement, as I am noticing. I am going to look up some tutorials on how to do this better.


if(gPlayer.CurrentState == gPlayer.WALK_LEFT)
	{
		//gPlayer.CurrentFrame++;
		//gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8;
		SDL_RenderCopy(GameRend,gPlayer.LeftTex[gPlayer.CurrentFrame  % 8],NULL,&gPlayer.LeftRect[gPlayer.CurrentFrame  % 8]);
		if(gPlayer.CurrentFrame >= 8)
		{
			gPlayer.CurrentFrame = 1;
		}
	}

I noticed that when I put the % 8 inside the []'s it works a tiny bit better. However the animation is still a little off. The Player moves, but only so far, then it gets stuck.

This topic is closed to new replies.

Advertisement