Hey, so this is my first time dabbling in XNA, or any kind of movement code since the Macromedia Flash days.
Long story short... I've created a small game sprite that can move in any direction across a 2D plane.
I have added the following function, called moveSprite, which does as its name would suggest.
The code works using the if without elseif.... right up until I hit MAX_SPEED in any direction. At that point I lose the ability to adjust the speed (the if statement is no longer entered).
IE if I'm moving at full speed upwards (-10.0) and try to hit down, my speed will not decrease by the specificed increment (0.1, making it -9.9).
I added the following elseif code to try and combat it, but it did not work:
The logic here is that if we press the up key and the sprite is moving at full speed downward along the Y axis (+10.0) we should allow the speed change (+10.0 - 0.1 = 9.9) causing deceleration. At the next frame, the if statement logic should sufficiently cover any scenario. Adding these elseif statements has had no effect on sprite movement.
...and before you get on me about how bad this movement code logic is... yes, I know, it's bad. I'm eventually going to scrap it and create a system that better matches a spaceship (up propels the ship forward, where forward is relative to where the front of the ship is... I figure I'll have to calculate the angle of rotation and adjust X and Y position using some kind of math).
I just really want to figure out why this won't work before I do that. Any help is appreciated =)
Long story short... I've created a small game sprite that can move in any direction across a 2D plane.
I have added the following function, called moveSprite, which does as its name would suggest.
Code:
void moveSprite(GameTime gameTime)
{
KeyboardState newState = Keyboard.GetState();
//problem here... if I hit acceleration of 10.0f I lose the ability to control the sprite
if (newState.IsKeyDown(Keys.Up) && spriteSpeed.Y < MAX_SPEED_PLUS && spriteSpeed.Y > MAX_SPEED_NEG)
spriteSpeed.Y -= SPEED_INCREM; //speed increment is 0.1
else if (newState.IsKeyDown(Keys.Up) && spriteSpeed.Y >= MAX_SPEED_PLUS)
spriteSpeed.Y -= SPEED_INCREM;
if (newState.IsKeyDown(Keys.Down) && spriteSpeed.Y < MAX_SPEED_PLUS && spriteSpeed.Y > MAX_SPEED_NEG)
spriteSpeed.Y += SPEED_INCREM;
else if (newState.IsKeyDown(Keys.Down) && spriteSpeed.Y <= MAX_SPEED_NEG)
spriteSpeed.Y += SPEED_INCREM;
if (newState.IsKeyDown(Keys.Left) && spriteSpeed.X < MAX_SPEED_PLUS && spriteSpeed.X > MAX_SPEED_NEG)
spriteSpeed.X -= SPEED_INCREM;
else if (newState.IsKeyDown(Keys.Left) && spriteSpeed.Y >= MAX_SPEED_PLUS)
spriteSpeed.X -= SPEED_INCREM;
if (newState.IsKeyDown(Keys.Right) && spriteSpeed.X < MAX_SPEED_PLUS && spriteSpeed.X > MAX_SPEED_NEG)
spriteSpeed.X += SPEED_INCREM;
else if (newState.IsKeyDown(Keys.Right) && spriteSpeed.Y <= MAX_SPEED_NEG)
spriteSpeed.X += SPEED_INCREM;
spritePosition.X += spriteSpeed.X;
spritePosition.Y += spriteSpeed.Y;
}
The code works using the if without elseif.... right up until I hit MAX_SPEED in any direction. At that point I lose the ability to adjust the speed (the if statement is no longer entered).
IE if I'm moving at full speed upwards (-10.0) and try to hit down, my speed will not decrease by the specificed increment (0.1, making it -9.9).
I added the following elseif code to try and combat it, but it did not work:
Code:
else if (newState.IsKeyDown(Keys.Up) && spriteSpeed.Y >= MAX_SPEED_PLUS)
spriteSpeed.Y -= SPEED_INCREM;
...and before you get on me about how bad this movement code logic is... yes, I know, it's bad. I'm eventually going to scrap it and create a system that better matches a spaceship (up propels the ship forward, where forward is relative to where the front of the ship is... I figure I'll have to calculate the angle of rotation and adjust X and Y position using some kind of math).
I just really want to figure out why this won't work before I do that. Any help is appreciated =)