0

I'm a beginner in java and im suffering to make a 2d game.
I already wrote a simple code that makes the character or the sprite jump by increment his Yposition when key is pressed and do the reverse when released ,but it doesn't satisfy me because i don't want the sprite to fall the same speed he fly with it, i want to make fall slowly ,i mean speed (Y increment) of fllying is separated from the falling one.

this may explain what im saying :
1-fllying https://jsfiddle.net/54xfevoc/embedded/result/
2- falling https://jsfiddle.net/poL9cowz/embedded/result/

thank you.

GoForIt
  • 3
  • 3

2 Answers2

1

When I make a physics part of a game, Personally I don't like to fix or modify basic physics constants. ex) gravity.

So, I suggest you to add delta time concept in your physics.
When the object jumps up, time is passing fast. When the object goes down, the time become slow.

I modified your js fiddle sample source. this is not exactly what I want to say. but it could be a example.

Run Sample
JS Source

Jinbom Heo
  • 862
  • 1
  • 6
  • 15
0

Well, the fix I would suggest would be adding this to your update method:

if (velocityY <= 0)
{
    gravity = 0.3;        
}
else
{
    gravity = 0.1;
}

Pretty straight-forward in my opinion. I looked at your falling program and it seemed to lack acceleration. Was that on purpose? If you really want to do that, you can change your update method to:

function Update()
{
    if(gravity == 0.0)
    {
        positionY += 0.3
    }
    else
    {
        velocityY += gravity;
        positionY += velocityY;
    }
    if(positionY > 175.0)
    {
        positionY = 175.0;
        velocityY= 0.0;
        onGround = true;
    }
    if (velocityY <= 0)
    {
        gravity = 0.3;        
    }
    else
    {
        gravity = 0.0;
    }

    if(positionX < 10 || positionX > 190)
        velocityX *= -1;
}

but in my opinion that's not very realistic at all, and it's really easy to get stuck in the clouds (way too far above the screen) in my opinion.

I suggest you take a look at some physics documentation for acceleration, it should help you understand more of the velocity-type problems you're having in the future.

Hope this helps!

Superdoggy
  • 527
  • 4
  • 16
  • I'm sorry for the way i wrote the question, the code i have put in the link is not mine i take it from link italic bold code just to explain. but Yes your suggestion helps me. – GoForIt Mar 14 '15 at 09:00