Page 1 of 1

XNA Movement Question

Posted: Fri Jan 27, 2012 6:01 am
by mattykins
So I was playing around in XNA earlier, and I felt like the movement of the sprite was really choppy. So I tried to set up a sort of smoothing code

Code: Select all

 private void UpdateInput()
        {
            KeyboardState newState = Keyboard.GetState();
            
            if (newState.IsKeyDown(Keys.Space))
            {
                for (int i = 0; i > 10; i++)
                {
                    PlayerXY.X += i;
                    UpdateLoc();
                }
                
                
                if (!oldState.IsKeyDown(Keys.Space))
                {
                    
                }
            }
            else if (oldState.IsKeyDown(Keys.Space))
            {
                
                for (int o = 10; o < -1; o--)
                {
                    PlayerXY.X += o;
                    UpdateLoc();
                }
            }

           
            oldState = newState;
            
            
        }
        private void UpdateLoc()
        {
            position = new Vector2(PlayerXY.X, PlayerXY.Y);
        }
And its setup in a gameloop that looks Like

Code: Select all

UpdateInput();
UpdateLoc();
It worked perfect when I just had the PlayerXY.X +=50; instead of the For loop. However when I use the for loop nothing happens when I press the space key.

Think you guys know whats wrong? Thanks! :)

Re: XNA Movement Question

Posted: Fri Jan 27, 2012 4:04 pm
by Xaleph
Did you update the Draw() method? If so, does Player get the new Postion Vector2 passed trough?

Re: XNA Movement Question

Posted: Fri Jan 27, 2012 4:21 pm
by mattykins
The UpdateLoc() method you see in there calls a method I wrote that updatesit for me.

Re: XNA Movement Question

Posted: Fri Jan 27, 2012 9:04 pm
by Jackolantern
Are you using a velocity system? If not, the movement will be terrible because of the keyboard buffer.

Re: XNA Movement Question

Posted: Fri Jan 27, 2012 9:11 pm
by mattykins
Yeah, that was the point of the for loops, was to make a velocity system. So everytime it iterated through the loop it gained +1 speed till the speed capped out at 5. Then when you let go it would go through a loop that would -1 speed everytime it iterated till the speed equaled 0.