Search code examples
javaslick2d

My jumping is way to fast, its over in the blink of an eye


the title says alot, ive tried thread.sleep before and it does not help. I want to slow down the jumping command so that you can see the character jump, right now it finishes in the blink of an eye. Please explain how to do this. I use slick2d java, new to java

Thanks for your time!

package JavaGame;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Play extends BasicGameState{

    Animation bucky, movingUp, movingDown, movingLeft, movingRight;

    boolean quit = false;
    int[] duration = {400, 100}; //2 tenths of a second and 2 tenths of a second, both in under half a second. Series of image, how long each image lasts  
    //the array of numbers decides the length of the animation
    float buckyPositionX = 0;
    float buckyPositionY = 0;
    float shiftX = buckyPositionX + 540; //Shifting something 320, always letting bucky be in the middle of the screen
    float shiftY = buckyPositionY + 620;
    boolean jumping = false;
    double gravity = 10;
    double jumpingTime = 200;
    float v = shiftY;
    double counter2 = 4;

    public Play(int state){

    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
        Image[] walkUp = {new Image("res/buckysRight.png"), new Image("res/buckysFront.png")}; //Change to jump, add more imagines (same amount of duration as images)
        Image[] walkDown = {new Image("res/buckysFront.png"), new Image("res/buckysFront.png")}; //change last picture to the front will restore it to the front frame ending the animation
        Image[] walkLeft = {new Image("res/buckysLeft.png"), new Image("res/buckysFront.png")};
        Image[] walkRight = {new Image("res/buckysRight.png"), new Image("res/buckysFront.png")};

        //Creating the actual animation, animations defined Animation, bucky bla bla
        movingUp = new Animation(walkUp, duration, false);
        movingLeft = new Animation(walkLeft, duration, false);
        movingRight = new Animation(walkRight, duration, false);
        movingDown = new Animation(walkDown, duration, false);
        bucky = movingDown;
    }


    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
        g.fillRect(buckyPositionX, buckyPositionY, 10000, 50); //Øverste boks
        g.fillRect(buckyPositionX, buckyPositionY + 660, 10000, 70); //Nederste boks
        g.fillRect(buckyPositionX, buckyPositionY + 360, 50, 300); // Bakerste Kant

        bucky.draw(shiftX, shiftY);
        boolean debug = true;

        if(debug == true){
            g.drawString("Buckys X: " + buckyPositionX + "\nBuckys y: " +buckyPositionY, 900, 50);
            g.drawString("Buckys 2 X: " + shiftX + "\nBuckys 2 y: " + shiftY, 900, 100);
        }

        if(quit == true){ //When esc is hit launch the menu.
            g.drawString("Resume (R)", 250, 100);
            g.drawString("Main Menu (M)", 250, 150);
            g.drawString("Quit (Q)", 250, 200);

            if(quit == false){
                g.clear();
            }
        }
    }


    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
        Input input = gc.getInput();

        if(input.isKeyDown(Input.KEY_RIGHT)){
            bucky = movingRight;
            buckyPositionX -= .2;
        }

        if(input.isKeyDown(Input.KEY_LEFT)){
            bucky = movingLeft;
            buckyPositionX += .2;

            if(buckyPositionX > 492){
                buckyPositionX -= .2;
            }//delta * .1f
        }

        if(shiftY < 620 && jumping != true){
            shiftY += gravity;  
        }

        if(input.isKeyPressed(Input.KEY_SPACE)){
            jumping = true;

            while(jumping == true){
                counter2 += 0.0005;
                shiftY = shiftY - (int) ((Math.sin(counter2) + (Math.cos(counter2)))*20);

                if(counter2 >= 7){
                    counter2 = 4;
                    jumping = false;
                }
            }
        }

        //escape
        if(input.isKeyDown(Input.KEY_ESCAPE)){
            quit = true;
        }

        //when the menu is up
        if(quit == true){
            if(input.isKeyDown(Input.KEY_R)){
                quit = false;
            }

            if(input.isKeyDown(Input.KEY_M)){
                sbg.enterState(0);
            }

            if(input.isKeyDown(Input.KEY_Q)){
                System.exit(0);
            }
        }   
    }

    public int getID(){
        return 1;
    }       
}

Solution

  • You should always make game update depending on TIME, so it has same speed on all computers without problem. And it solves your problem too.

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
    

    The game container should have something liek this :

    gc.getTime()
    

    Which shows you how much time passed in the last run. If it does not have this, you can use

    System.nanoTime()
    

    Then you only need some int value, where you store your time and you do some update for example only once each 50 milisec.