Xbox LIVE Indie Games
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 1 (7 posts)

Moving a Character

Last post 10/16/2009 12:35 AM by india103. 6 replies.
  • 1/21/2009 10:10 PM

    Moving a Character

    Hi everyone! I have a question, I just started learning XNA. I've learned how to display a Sprite onto the screen, but I'm confused as to why my code won't allow me to move the sprite. Here's what I have in the Update class:

    KeyboardState keys = Keyboard.GetState();
    if (keys.IsKeyDown(Keys.Right)) mSprite.Position.X = mSprite.Position.X++;

    Why won't this move the sprite to the right when using the right key? Thank you very much for your help everyone! I'm trying to make a simple 2D game where you have to collect items, but you're only able to move the character to the left and the right. Thanks again!
  • 1/21/2009 10:26 PM In reply to

    Re: Moving a Character

    You are using the ++ post-fix operator.

    This is how the code runs when the key is pressed:
    store original value of X
    increment X
    Set X = original value of x

    You want either
    mSprite.Position.X++;
    or
    mSprite.Position.X = mSprite.Position.X + 1;
    or
    mSprite.Position.X += 1;

    For more info on how the post-fix increment operation works, read this article
  • 1/21/2009 10:35 PM In reply to

    Re: Moving a Character

    Ah you're right! User error! :)

    Thank you very much! If I may ask you one more question:

    How do I simulate jumping? The character is starting at the Y position 280, and I'd like him to jump to 285, and then gradually back down. Is there a way to create a loop that has a pause in it that allows me to see the jump? I tried to do the following code, but it goes by so quick that you don't get to see him jump! Thank you very much!

                if (keys.IsKeyDown(Keys.Space))
                {
                    mSprite.Position.Y = 281;
                    mSprite.Position.Y = 282;
                    mSprite.Position.Y = 283;
                    mSprite.Position.Y = 284;
                    mSprite.Position.Y = 285;
                    mSprite.Position.Y = 284;
                    mSprite.Position.Y = 283;
                    mSprite.Position.Y = 282;
                    mSprite.Position.Y = 281;
                    mSprite.Position.Y = 280;
                }

    I'm sorry I'm actually trying! I know that coding it like this would probably be frowned upon, but I'm not sure how to do it and I can't find a tutorial that makes sense on how to. Thank you again!
  • 1/21/2009 11:15 PM In reply to

    Re: Moving a Character

    Brandon Coonrad:
    Ah you're right! User error! :)

    Thank you very much! If I may ask you one more question:

    How do I simulate jumping? The character is starting at the Y position 280, and I'd like him to jump to 285, and then gradually back down. Is there a way to create a loop that has a pause in it that allows me to see the jump? I tried to do the following code, but it goes by so quick that you don't get to see him jump! Thank you very much!

                if (keys.IsKeyDown(Keys.Space))
                {
                    mSprite.Position.Y = 281;
                    mSprite.Position.Y = 282;
                    mSprite.Position.Y = 283;
                    mSprite.Position.Y = 284;
                    mSprite.Position.Y = 285;
                    mSprite.Position.Y = 284;
                    mSprite.Position.Y = 283;
                    mSprite.Position.Y = 282;
                    mSprite.Position.Y = 281;
                    mSprite.Position.Y = 280;
                }

    I'm sorry I'm actually trying! I know that coding it like this would probably be frowned upon, but I'm not sure how to do it and I can't find a tutorial that makes sense on how to. Thank you again!


    Basically, the probem with your current code is it processes every single mSprite.Position.Y = XXX call every frame, meaning it will always end on 280, then draw it in the same spot on the Y axis every time.

    Create a new Project, choose the Platformer Starter Kit.  It'll take a second, but when it's created, go check out the Player class (Player.cs) and look for the DoJump() method.  That will show you how to do the jump properly.

  • 1/22/2009 12:20 AM In reply to

    Re: Moving a Character

    Brandon Coonrad:
    Hi everyone! I have a question, I just started learning XNA. I've learned how to display a Sprite onto the screen, but I'm confused as to why my code won't allow me to move the sprite. Here's what I have in the Update class:

    KeyboardState keys = Keyboard.GetState();
    if (keys.IsKeyDown(Keys.Right)) mSprite.Position.X = mSprite.Position.X++;

    Why won't this move the sprite to the right when using the right key? Thank you very much for your help everyone! I'm trying to make a simple 2D game where you have to collect items, but you're only able to move the character to the left and the right. Thanks again!


    I just starting learning XNA aswell, but I still have not figured out how to make my sprite move using the keypad of my keyboard. I know how to load the sprite and I know how to draw it to the screen. Can anyone help please?
  • 1/22/2009 12:34 AM In reply to

    Re: Moving a Character

    Kripticcc:

    I just starting learning XNA aswell, but I still have not figured out how to make my sprite move using the keypad of my keyboard. I know how to load the sprite and I know how to draw it to the screen. Can anyone help please?


    Much like how to jump, creating the Platformer Starter Kit will show you how to move a sprite across the screen.  To create the Platformer starter kit, create a new project, under the XNA category, you should see "Platformer Starter Kit" which will create a game with a name of "Platformer1".

    Looking at this game, the one part that might confuse someone new to XNA is the Level, the Animation and the Animation Classes.  You can pretty much ignore these and move right to the Player class (Player.cs) and start there.  I beleive there is a "HandleInputs()" method which the game uses to figure out if a movement key is pressed.

    From there, what I'd do is also open your current project (the one where you loaded a sprite but can't make it move) and start editting that project.  If you only care about left and right (which is a good place to start), you really should only care about your sprites current Position (a 2D vector) and it's velocity (another 2D vector) which you will use in conjuncture with the GameTime to establish the new postion of the sprite prior to it being redrawn later in the frame.

    Once you get that moving left and right, you can start implementing more of the Platformers code into your own project.  Personally, I'd wouldn't worry about animating your sprites until you have basic movement and collision detection implemented.




     

  • 10/16/2009 12:35 AM In reply to

    Re: Moving a Character

    Here is how I made my sprite move:

    in Update:

    if (Keyboard.GetState().IsKeyDown(Keys.Right))
    {
          positionX += 1;
    }
    if (Keyboard.GetState().IsKeyDown(Keys.Left))
    {
          positionX -= 1;
    }
Page 1 of 1 (7 posts) Previous Discussion Next Discussion