XNA Movement Question

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

XNA Movement Question

Post 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! :)
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: XNA Movement Question

Post by Xaleph »

Did you update the Draw() method? If so, does Player get the new Postion Vector2 passed trough?
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: XNA Movement Question

Post by mattykins »

The UpdateLoc() method you see in there calls a method I wrote that updatesit for me.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: XNA Movement Question

Post by Jackolantern »

Are you using a velocity system? If not, the movement will be terrible because of the keyboard buffer.
The indelible lord of tl;dr
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: XNA Movement Question

Post 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.
Post Reply

Return to “Beginner Help and Support”