Animating With Separate Image Files

Started by
14 comments, last by LeftyGuitar 9 years, 9 months ago

What's the type for X_Pos and Y_Pos? Are they int or float?

EDIT: I ask because your value assignments make it look like they're still int. This will have the same problem as before (no change in position), if X_Vel * MOVEMENT_SPEED is less than 1 (and won't behave as expected except for values that can be represented as integers).

Hello to all my stalkers.

Advertisement

Sorry, I should have specified the variable types as well.

X_Pos and Y_Pos are ints.

X_Vel and Y_Vel are floats.

I also did trying changing gAidan.WalkingDstRect[0].x = gAidan.X_Pos to gAidan.WalkingDstRect[gAidan.CurrentFrame].x = gAidan.x_Pos, but that didn't seem to help me.

X_Pos and Y_Pos are ints

Well ...

What you need to do instead is:
With gAidan.X_Pos being a float, and gAidan.X_Vel being a float, and MOVEMENT_SPEED being a float:
...

1.) updating position (notice that only floats are used, so above problem is avoided)

gAidan.X_Pos -= gAidan.X_Vel * MOVEMENT_SPEED;

(bolded for emphasizing). The accumulation need to be done on float if you want to have an effect at all when increasing by values less than 1.

OK, I'm just puzzled here. Here is a bigger chunk of code, perhaps I am doing something wrong, and not noticing it? I have the gAidan.WalkingDstRect[].x = gAidan.X_Pos,

same for the Y coordinate rectangle box as well. I've left the width and height of it by 400x400. So it looks like this

gAidan.WalkingDstRect[0].x = gAidan.X_Pos;

gAidan.WalkingDstRect[0].y = gAidan.Y_Pos;

gAidan.WalkingDstRect[0].w = 400;

gAidan.WalkingDstRect[0].h = 400;

I've tried a lot of different things, but for whatever reason, it just won't move. It stays in place while the animation plays.


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

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

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

				if(event.key.keysym.sym == SDLK_LEFT)
				{
					gAidan.Action = gAidan.WALKING;
					gAidan.X_Pos -= gAidan.X_Vel * DeltaTime;
				}
				else if(event.key.keysym.sym == SDLK_RIGHT)
				{
					gAidan.Action = gAidan.WALKING;
					gAidan.X_Pos += gAidan.X_Vel * DeltaTime;
				}

				if(event.key.keysym.sym == SDLK_UP)
				{
					gAidan.Action = gAidan.JUMP_A;
				}
				else if(event.key.keysym.sym == SDLK_DOWN)
				{
					gAidan.Action = gAidan.CROUCH;
				}
			}

			if(event.type == SDL_KEYUP && event.key.repeat == 0)
			{
				if(event.key.keysym.sym == SDLK_LEFT)
				{
					gAidan.X_Vel = 0.0f;
					gAidan.Action = gAidan.STANDING;
					
				}
				else if(event.key.keysym.sym == SDLK_RIGHT)
				{
					gAidan.X_Vel = 0.0f;
					gAidan.Action = gAidan.STANDING;
					
				}
			}
		}

		Update();

		Render();
	}
}

void Update()
{
	gAidan.WalkingDstRect[gAidan.CurrentFrame].x = (int)gAidan.X_Pos;
}

void Render()
{
		SDL_RenderClear(MainRend);

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

		if(gAidan.Action == gAidan.STANDING)
		{
			SDL_RenderCopy(MainRend,gAidan.StandTex[gAidan.CurrentFrame],NULL,&gAidan.StandDstRect[gAidan.CurrentFrame]);

			gAidan.CurrentFrame++;
			gAidan.CurrentFrame = SDL_GetTicks() / 100 % 5;
		}

		if(gAidan.Action == gAidan.WALKING)
		{
			SDL_RenderCopy(MainRend,gAidan.WalkingTex[gAidan.CurrentFrame], NULL, &gAidan.WalkingDstRect[gAidan.CurrentFrame]);

			gAidan.CurrentFrame++;
			gAidan.CurrentFrame = SDL_GetTicks() / 100 % 5;
		}

		SDL_RenderPresent(MainRend);
}

You are only modifying its position when an event is polled.
That means every time you press SDLK_LEFT it moves “gAidan.X_Vel * DeltaTime” exactly once.
Even if gAidan.X_Vel is not 0, DeltaTime is likely going to be around 0.1666 or something, so you are probably moving less than a pixel.

But that’s assuming gAidan.X_Vel is not 0. But it is 0. You set it to 0 in many places. You set it to non-zero in no places.



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

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

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

				if(event.key.keysym.sym == SDLK_LEFT)
				{
					gAidan.Action = gAidan.WALKING;
					gAidan.X_Vel = -20.0f;
				}
				else if(event.key.keysym.sym == SDLK_RIGHT)
				{
					gAidan.Action = gAidan.WALKING;
					gAidan.X_Vel = 20.0f;
				}

				if(event.key.keysym.sym == SDLK_UP)
				{
					gAidan.Action = gAidan.JUMP_A;
				}
				else if(event.key.keysym.sym == SDLK_DOWN)
				{
					gAidan.Action = gAidan.CROUCH;
				}
			}

			if(event.type == SDL_KEYUP && event.key.repeat == 0)
			{
				if(event.key.keysym.sym == SDLK_LEFT)
				{
					gAidan.X_Vel = 0.0f;
					gAidan.Action = gAidan.STANDING;
					
				}
				else if(event.key.keysym.sym == SDLK_RIGHT)
				{
					gAidan.X_Vel = 0.0f;
					gAidan.Action = gAidan.STANDING;
					
				}
			}
		}
 		gAidan.X_Pos += gAidan.X_Vel * DeltaTime;
		Update();

		Render();
	}
}



L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Thanks. That got it to move a little, but it only moves about a few pixels, then it stays still and plays the animations.

This topic is closed to new replies.

Advertisement