java - 我跳的太快了,一眨眼就结束了

标签 java slick2d

标题说了很多,我之前尝试过 thread.sleep 但没有帮助。我想放慢跳跃命令的速度,这样你就可以看到角色跳跃,现在眨眼就完成了。请解释如何执行此操作。我使用 slick2d java,java新手

感谢您的宝贵时间!

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;
    }       
}

最佳答案

您应该始终根据时间进行游戏更新,这样它在所有计算机上的速度都相同,没有问题。它也解决了您的问题。

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

游戏容器应该有这样的东西:

gc.getTime()

这会显示上次运行所用的时间。如果没有这个,你可以使用

System.nanoTime()

然后你只需要一些 int 值,在其中存储你的时间并进行一些更新,例如每 50 毫秒更新一次。

关于java - 我跳的太快了,一眨眼就结束了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19336255/

相关文章:

java - java 打开一定数量的文件

java - Guice:绑定(bind)具有不同依赖关系的多个对象

java - 错误:For input string: "" when I add object layer to my tilemap

java - 将 SWT 元素与 Slick2D 结合使用

java - Slick2d True Type 字体的自动换行

java - 情感分析工具的停用词库

java - 为什么 Jenkins 的启动脚本在 Java 11 (Ubuntu) 下失败?

java - 从 JOptionPane 中删除标题栏

java - 在 mousePressed 处更改 2 个图像

java - 使用着色器时纹理不渲染?