XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

Sprite jump

Last post 07/11/2009 10:51 by tredge. 3 replies.
  • 05/11/2009 19:12

    Sprite jump

    Hi. I am new to the forum. I am struggling to get my head around the jump function. I am creating a platform game and was wondering if anyone can help me on this?

    Basically, what I need is the sprite to move up and then, say a velocity value, takes it back down gradually.  Being new to programming, I'm not sure about the calls from the main Game.cs file

    Thanks a lot
  • 05/11/2009 20:37 In reply to

    Re: Sprite jump

    try george's tutorials, which also contain a part about jumping.
  • 06/11/2009 17:57 In reply to

    Re: Sprite jump

    One thing you'll need to learn eventually when you program games is kinematics.  Generally, most objects in games have the following properties: position, velocity, and acceleration.  Each time you call update you update the position based on velocity, and you update velocity based on acceleration.  It would look something like this.


    position += velocity
    velocity += acceleration

    If you only need your object to change direction instantaneously, then acceleration isn't needed.  However, since you what your character to fall down gradually like objects do in real life, you will need acceleration.   If your Y-component represents the up/down direction, you give your object's acceleration a negative value in it's Y component.  

    Your update code could look something like this:

    position += velocity
    veloctiy.y -= gravityConstant


  • 07/11/2009 10:51 In reply to

    Re: Sprite jump

    thanks caedicus, I think I know what you mean.  So basically, I will have a class called player which creates the player when called from the Game.cs file.  This player class will have a method called updatePlayer and here I will run 2 methods, as shown below:

    public

     

    void checkForInput()

     

    {

    currentKeyBoardState =

    Keyboard.GetState();

     

     

    // if the user presses space bar, move the sprite up the screen by 50

     

     

    if (currentKeyBoardState.IsKeyDown(Keys.Space))

     

    {

    ashPosition.Y += velocity;

    }

    }

     

    public void applyGravity()

     

    {

     

    // take away gravity from the velocity value

     

    velocity.Y -= gravity;

     

    // add velocity to the sprite, hence moving it back down the screen

     

    charPosition.Y += velocity.Y;

    }


    is this the correct setup? as for calling it from the main method, am I right in saying you call char.updatePlayer() from the update method in the Game.cs file?

Page 1 of 1 (4 items) Previous Next