How to make sprite jump in java?

前端 未结 2 2041
忘了有多久
忘了有多久 2020-11-27 08:56

I have KeyEvents for a sprite for moving left, right, up and down. I was just messing around and was thinking ahead for another project in which I want the sprite to jump. I

2条回答
  •  鱼传尺愫
    2020-11-27 09:05

    Well I can think of one way. It involves some complex amounts of math (parabola). So i'm going to provide a very simple answer.

    int y = 0;
    

    and in the method that tests for the spacebar...

    if (y !< 1){
        if (y < 30){
    
       y += 1;
    
       }
    
       if (y > 30){
    
        y -= 1;
    
    
        }
    }
    

    I haven't tested it out yet, but it should work in theory....But it won't animate anything, this code is only going to take the sprites Y value and make it go up. That's just about the easiest jumping method that can exist....

提交回复
热议问题