无法将我的方法与Slick库一起使用

时间:2014-04-30 12:47:44

标签: java

所以我前段时间做了一个跳跃的java游戏,我用这个方法进行了所有移动:

double height = 0, speed = 4;
    public static final double gravity = 9.81;
    double x = 25;
    int a;
    int y = (int) (500-(height*100));
    boolean left = false, right = false, up = false;

public void the_jump() {
        long previous = 0, start = 0;
        while(true){
            start = System.nanoTime();
            if(previous != 0 && up){
                double delta = start - previous;
                height = (height + (delta/1000000000) * speed);        
                speed -= (delta/1000000000)  * gravity;
                y = (int) (500-(height * 100));
            }  
            if(left){
                x-= 3;
            }    
            if(right){
                x+= 3;
            }    
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(height < 0){
                height = 0;
                speed = 4; 
                up = false;
            }      
            previous = start;
        }
    }   

现在我只用JComponents这样做就可以了,但是现在当我想在Slick环境中实现它时,它失败了。

问题出在while(true){}循环中。如果我针对for(int i = 0; i < 1; i++)循环更改它,那么左右移动将起作用。但这不适用于跳跃。我可以增加i < 1 to i < 5,然后跳转就可以了,但代价是很多性能。

那么人们将如何在光滑中实现这一点?现在我在the_jump();方法中调用public void update(GameContainer gc, int t) throws SlickException,如果我使用while循环,游戏就会崩溃。

1 个答案:

答案 0 :(得分:1)

已在update(GameContainer gc, int delta)循环播放,您必须将while循环中的所有代码放入update方法。
此外,您将获得两次更新之间的增量时间作为参数,因此无需计算它。

随意问我更多问题;)

关闭主题,你知道Slick2d是否仍然存在吗?我几个月前切换到libGDX,我真的建议你去测试它,这太有趣了:))