Jump to content

  • Log In with Google      Sign In   
  • Create Account

Animation Plays, Character Stays In Place


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 LeftyGuitar   Members   -  Reputation: 186

Like
0Likes
Like

Posted 14 August 2014 - 03:10 PM

Hello, I have it so that when the user presses one of the arrow keys, the animation will play, but the character does not move. It just stays in place.

const int MAX_WIDTH = 1024;
const int MAX_HEIGHT = 720;

gPlayer.XPos = MAX_WIDTH / 2;
	gPlayer.YPos = MAX_HEIGHT / 2;
	gPlayer.XVel = 0.010f;
	gPlayer.YVel = 0.010f;
	gPlayer.CurrentFrame = 0;
	gPlayer.CurrentState = gPlayer.IDLE;
	gPlayer.Facing = gPlayer.DOWN;

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;
	}

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;
					gPlayer.CurrentState = gPlayer.WALK_UP;
				}
				else if(event.key.keysym.sym == SDLK_DOWN)
				{
					gPlayer.Facing = gPlayer.DOWN;
					gPlayer.CurrentState = gPlayer.WALK_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;
					gPlayer.CurrentState = gPlayer.WALK_RIGHT;
				}
			}

			if(event.type == SDL_KEYUP)
			{
				if(event.key.keysym.sym == SDLK_UP)
				{
					gPlayer.CurrentState = gPlayer.IDLE;
					gPlayer.Facing = gPlayer.UP;
					gPlayer.YVel = 0.0f;
				}
				else if(event.key.keysym.sym == SDLK_DOWN)
				{
					gPlayer.CurrentState = gPlayer.IDLE;
					gPlayer.Facing = gPlayer.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.CurrentState = gPlayer.IDLE;
					gPlayer.Facing = gPlayer.RIGHT;
					gPlayer.XVel = 0.0f;
				}

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

	if(gPlayer.CurrentState == gPlayer.WALK_RIGHT)
	{
		gPlayer.XPos += gPlayer.XVel * DeltaTime;
		gPlayer.LeftRect[gPlayer.CurrentFrame % 8].x = gPlayer.XPos;
		gPlayer.XPos = gPlayer.XPos;
	}

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

	if(gPlayer.CurrentState == gPlayer.WALK_RIGHT)
	{
		SDL_RenderCopyEx(GameRend,gPlayer.LeftTex[gPlayer.CurrentFrame % 8],NULL,&gPlayer.LeftRect[gPlayer.CurrentFrame % 8],NULL,NULL,SDL_FLIP_HORIZONTAL);
	}

I have posted the code that may be needed. I just am not sure why it would stay in place. I have looked over the code and looked at some tutorials, but I can't figure out why it is just staying in place.



Sponsor:

#2 Sponji   Members   -  Reputation: 1354

Like
0Likes
Like

Posted 14 August 2014 - 03:41 PM

Is that top part your initialization? Why do you initialize the velocities to 0.01f? I think you want to set those to zero. And the actual problem you're having is probably because you don't change the velocities when a key goes down.


Edited by Sponji, 14 August 2014 - 03:41 PM.

Derp

#3 Lactose!   GDNet+   -  Reputation: 3813

Like
2Likes
Like

Posted 14 August 2014 - 03:46 PM

Except for initialization, you never set the velocities to a non-zero value. This is also a place where it doesn't make sense to initialize it.

Upon key release, you do set the velocoties to zero.

 

In addition, a likely issue is also that your position variables are int, and your DeltaTime variable might be a float with value equal to something like 0.016.

If so, the velocity * DeltaTime code will be cast into an int that's zero (unless your velocities are very large), meaning no position change.

 

You'll need to have your position values as type float, to be able to move in most velocities.

If you need the position in ints due to rendering positions and such, you can cast it from float to int when you need it,



#4 LeftyGuitar   Members   -  Reputation: 186

Like
0Likes
Like

Posted 14 August 2014 - 03:54 PM

OK, so I got it to move, somewhat, however when the key is released, it just goes back to the position, from where it started.

for(int i = 0; i <= 8; i++)
	{
		gPlayer.LeftRect[i].x = (int)gPlayer.XPos;
		gPlayer.LeftRect[i].y = (int)gPlayer.YPos;
		gPlayer.LeftRect[i].w = gPlayer.Width;
		gPlayer.LeftRect[i].h = gPlayer.Height;
	}

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;
					gPlayer.CurrentState = gPlayer.WALK_UP;
				}
				else if(event.key.keysym.sym == SDLK_DOWN)
				{
					gPlayer.Facing = gPlayer.DOWN;
					gPlayer.CurrentState = gPlayer.WALK_DOWN;
				}
				else if(event.key.keysym.sym == SDLK_LEFT)
				{
					gPlayer.Facing = gPlayer.LEFT;
					gPlayer.CurrentState = gPlayer.WALK_LEFT;
					gPlayer.XVel = 10.0f;
				}
				else if(event.key.keysym.sym == SDLK_RIGHT)
				{
					gPlayer.Facing = gPlayer.RIGHT;
					gPlayer.CurrentState = gPlayer.WALK_RIGHT;
					gPlayer.XVel = 10.0f;
				}
			}

			if(event.type == SDL_KEYUP)
			{
				if(event.key.keysym.sym == SDLK_UP)
				{
					gPlayer.CurrentState = gPlayer.IDLE;
					gPlayer.Facing = gPlayer.UP;
					gPlayer.YVel = 0.0f;
				}
				else if(event.key.keysym.sym == SDLK_DOWN)
				{
					gPlayer.CurrentState = gPlayer.IDLE;
					gPlayer.Facing = gPlayer.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.CurrentState = gPlayer.IDLE;
					gPlayer.Facing = gPlayer.RIGHT;
					gPlayer.XVel = 0.0f;
				}
			}

if(gPlayer.CurrentState == gPlayer.WALK_LEFT)
	{
		gPlayer.XPos -= gPlayer.XVel * DeltaTime;
		gPlayer.LeftRect[gPlayer.CurrentFrame % 8].x = (int)gPlayer.XPos;
		gPlayer.XPos = gPlayer.XPos;
	}

	if(gPlayer.CurrentState == gPlayer.WALK_RIGHT)
	{
		gPlayer.XPos += gPlayer.XVel * DeltaTime;
		gPlayer.LeftRect[gPlayer.CurrentFrame % 8].x = (int)gPlayer.XPos;
		gPlayer.XPos = gPlayer.XPos;
	}


#5 Lactose!   GDNet+   -  Reputation: 3813

Like
2Likes
Like

Posted 14 August 2014 - 03:59 PM

That sounds like you set position to 0 somewhere in the code you haven't posted.

 

You're also doing some very strange things here, indicating you might want to slow down and be a bit more thorough. For example, you have a lot of gPlayer.XPos = gPlayer.XPos; which changes nothing. This to me suggests that you might want to clean up your code a bit -- it might even help you fix some bugs you have in the process.



#6 LeftyGuitar   Members   -  Reputation: 186

Like
0Likes
Like

Posted 15 August 2014 - 11:43 AM

	if(gPlayer.CurrentState == gPlayer.WALK_LEFT)
	{
		gPlayer.XPos -= gPlayer.XVel * DeltaTime;
		gPlayer.LeftRect[gPlayer.CurrentFrame % 8].x = (int)gPlayer.XPos;
		gPlayer.XPos = (float)gPlayer.LeftRect[gPlayer.CurrentFrame % 8].x; 
	}

	if(gPlayer.CurrentState == gPlayer.WALK_RIGHT)
	{
		gPlayer.XPos += gPlayer.XVel * DeltaTime;
		gPlayer.LeftRect[gPlayer.CurrentFrame % 8].x = (int)gPlayer.XPos;
		gPlayer.XPos = (float)gPlayer.LeftRect[gPlayer.CurrentFrame % 8].x;
	}

Thanks for the tips. I did the above, now the character moves, but the animation dosen't play when the character. It also still goes back to the origin point when the key is released. I only have the X and Y Positions set to origin values in the initialization method. Maybe I should start over with a better code base?



#7 osmanb   Crossbones+   -  Reputation: 1628

Like
0Likes
Like

Posted 15 August 2014 - 11:49 AM


Maybe I should start over with a better code base?

 

No. You should finish debugging your current code, to understand why it's doing what it's doing. I'd strongly suggest "Rubber Duck Debugging" (Google it), because I feel like you're making changes without really understanding what the code is supposed to be doing.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS