Let player jump from one square in a grid to another

Anything libgdx related goes here!

Let player jump from one square in a grid to another

Postby elterPro » Sun Sep 28, 2014 6:03 pm

I'm programming a game where you have to jump from one driftwood to another. You can see what I have here:

Image

Planks spawn at the top and scroll down. You can jump one tile up and one or two tiles left or right.

All elements (the planks and the player if he isn't jumping and standing on a plank) move every time
Code: Select all
draw(float delta)
is called:

Code: Select all
setY(getY() + FlotsamGroup.yVelocity * delta);


This works fine, but if the player starts to jump I want him to land on the exact same coordinates the plank in this location has. With my code, he doesn't and with the time it looks like this:

Image

You can see that the green lines around the player aren't at the same height from the surrounding planks.

I think I somehow have to use a modified version of the speed-code from above, but I also want to make the player jump faster as the planks scroll down, and someday I want to include, that the whole game gets faster with the amount of planks you already successfully jumped (decrease the
Code: Select all
FlotsamGroup.yVelocity
-Value)

Code: Select all
protected class JumpAction extends Action {
      
      Direction direction;
      Player player;
      Vector2 targetPosition;
      Vector2 scalarVector;
      
      public JumpAction(Direction direction) {
         this.direction = direction;
         Gdx.app.log("", "Go to " + direction);
         
         scalarVector = new Vector2(0, 1/2F);
         switch(direction) {
         case TWOLEFT:
            scalarVector.x = -2;
            break;
         case LEFT:
            scalarVector.x = -1;
            break;
         case UP:
            break;
         case RIGTH:
            scalarVector.x = 1;
            break;
         case TWORIGTH:
            scalarVector.x = 2;
            break;
         }
      }
      
      @Override
      public boolean act(float delta) {
         if(player == null) {
            player = (Player) getActor();
            player.isJumping = true;
            targetPosition = new Vector2(player.getX() + GameGrid.getSquareSize() * scalarVector.x, player.getY() + GameGrid.getSquareSize() * scalarVector.y);
            Gdx.app.log("", "Target Vector " + scalarVector.toString() + " | Target Position " + targetPosition.toString());
         }
         
         player.setX(player.getX() + ( scalarVector.x / -(FlotsamGroup.yVelocity * delta)) );
         player.setY(player.getY() + ( scalarVector.y / -(FlotsamGroup.yVelocity * delta)) );
         
         switch(direction) {
         case TWOLEFT:
         case LEFT:
            if(player.getX() < targetPosition.x)
               player.isJumping = false;
            break;
         case UP:
            if(player.getY() > targetPosition.y)
               player.isJumping = false;
            break;
         case RIGTH:
         case TWORIGTH:
            if(player.getX() > targetPosition.x)
               player.isJumping = false;
            break;
         }
         
         if(!player.isJumping) {
            player.setX(MathUtils.roundPositive(targetPosition.x));
            player.setY(MathUtils.roundPositive(targetPosition.y));
            return true;
         }
         return false;
      }
   }
elterPro
 
Posts: 1
Joined: Sun Sep 28, 2014 5:57 pm

Return to Libgdx

Who is online

Users browsing this forum: Google [Bot], MSN [Bot] and 1 guest