简体   繁体   English

使用Slick2D在Java中轻松进行

[英]Easing in Java with Slick2D

I am trying to make an object ease in and out. 我正在尝试使一个物体容易进出。

But when I render this it works well but I always get another object behaving directly opposite to the other. 但是,当我渲染它时,效果很好,但是我总是得到另一个与另一个对象完全相反的对象。

So when the object is heading up, the "ghost" object will be directly opposite heading down. 因此,当对象朝上时,“重影”对象将直接朝下。 It gives a rather weird screen tearing effect. 它给出了相当奇怪的屏幕撕裂效果。 I've tried turning on vsync but that seemed to make it worse. 我尝试打开vsync,但这似乎使情况变得更糟。 The object is a slick2d rectangle. 该对象是一个slick2d矩形。 It also has the habit of completely disappearing for a few seconds or even permanently turning into a straight line without me every changing its dimensions. 它也有几秒钟完全消失的习惯,甚至永久变成一条直线,而无需我改变尺寸。 This is the update code. 这是更新代码。

   @Override
public void update(GameContainer gc, StateBasedGame game, int delta) throws SlickException {
    // TODO Auto-generated method stub

    Input i=gc.getInput();
    d=2000/delta;
    if(i.isKeyDown(Input.KEY_DOWN)){
        if(frames<d)
        frames++;

        if(frames==1)b=hele.getY();
        hele.setY(Quad.easeInOut(frames, b, 720-b-50, d));//the 50 is the size of the sprite
        //c+=10;
    }
    else if(i.isKeyDown(Input.KEY_UP)){
        if(frames<d)
        frames++;

        if(frames==1)b=hele.getY();
        hele.setY(Quad.easeInOut(frames, b, -b, d));

    }



}

The ease in out formulas can be found here on github. 在出公式的难易程度,可以发现这里在GitHub上。

I have the frames var reset once the key is released elsewhere. 一旦将密钥释放到其他地方,我就将帧var重置。 I am trying to make the sprite behave like a helicopter. 我试图使精灵的行为像直升机一样。 If you have any idea how to make it more helicopter like, please share your knowledge! 如果您有任何想法如何使其更像直升机,请分享您的知识!

I seem to have found the problem, updating d with the delta every update causes the screen tear! 我似乎已经找到了问题,每次更新都用delta更新d会导致屏幕撕裂! Keeping it constant will solve the problem. 保持恒定将解决问题。

Instead of using a frame count I used a delta timer instead... 我没有使用帧计数,而是使用了增量计时器...

if(elapsedtime<d)
                elapsedtime+=delta;

that keeps track of the time using the delta and for every place I used frames, I replaced with the elapsed time. 可以使用增量跟踪时间,对于我使用框架的每个位置,我都将其替换为经过的时间。

if(frames==1){b=getCBox().getY();}
            getCBox().setY(Bounce.easeOut(elapsedtime, b, 720-b-getCBox().getHeight(), d));

However, I still used the frame count to identify the first update for the delta isn't always reliable. 但是,我仍然使用帧计数来确定增量的第一个更新并不总是可靠的。

The full source can be found on git here: https://bitbucket.org/BallisticBlaze/helirush-game/ 完整的源代码可以在git上找到: https : //bitbucket.org/BallisticBlaze/helirush-game/

Hope this helps anyone. 希望这对任何人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM