0

我已经建立了一个非常基本的平台游戏,我现在正在设置它。

除了跳跃之外,一切都很完美,我的跳跃长度非常不一致。它们的范围从非常短到非常长。

我不习惯使用 Slick2D、JWJGL 或 delta 整数,请帮忙!

这是我的整个关卡代码:

package game;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class LevelOne extends BasicGameState{

    Animation walkingLeft, walkingRight, still;
    int[] duration = {200,200,200};

    Image p;
    Image map;

    public String mouse = "Mouse not detected";
    public String gpos = "null";
    public int health = 1000;
    float CharX = 0;
    float CharY = 0;
    float PosX = CharX + 100;
    float PosY = CharY + 300;
    boolean jumping = false; 
    float verticalSpeed = 0.0f;


    public LevelOne(int state){

    }

    public void init(GameContainer gc, StateBasedGame sbg)throws SlickException{
        p = new Image("res/char.png");
        map = new Image("res/map.png");


        Image[] Left = {p.getSubImage(0,32,32,32), p.getSubImage(32,32,32,32), p.getSubImage(64,32,32,32)};
        Image[] Right = {p.getSubImage(0,64,32,32), p.getSubImage(0,64,32,32), p.getSubImage(0,64,32,32)};


        walkingLeft = new Animation(Left, duration, false);
        walkingRight = new Animation(Right, duration, false);
        still = walkingLeft;
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g)throws SlickException{
        g.setColor(Color.magenta);
        g.drawString("Developers Build", 650, 100);
        g.setColor(Color.white);
        g.drawString(mouse, 650, 150);
        g.drawString(gpos,650, 200);                   //Developers Build Sidebar thing
        g.drawString("Class: ", 650, 250);
        g.drawString("" + this.getClass(), 670, 270);
        g.drawString("State: " + this.getID(), 650, 300);
        g.drawString("Stage: Charlie", 650, 350);
        g.drawRect(620, 70, 400, 320);


        map.draw(CharX, CharY);                 //Floor



        g.setColor(Color.white);
        if(health < 500){
            g.setColor(Color.red);
        }
        g.drawString("Health: " + health, 10, 50);  
        g.setColor(Color.white);                                                //Health & Level
        g.drawString("Level: 1", 10, 70);


        still.draw(PosX, PosY);                       //Draw Character





    }

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

        int MouseX = Mouse.getX();
        int MouseY = Mouse.getY();
        mouse = "JPos: " + MouseX + " , " + MouseY;
        gpos = "GPos: " + MouseX + " , " + (460 - MouseY);


        if(input.isKeyDown(Input.KEY_D)){
            still = walkingRight;
            CharX -= delta * .2f;
        }

        if(input.isKeyDown(Input.KEY_A)){
            still = walkingLeft;
            CharX += delta * .2f;
            if(CharX > 100){
                CharX -= delta *.2f;
            }
        }

        if (input.isKeyPressed(Input.KEY_W) && !jumping) { 
            verticalSpeed = -1f * delta;//negative value indicates an upward movement 
            jumping = true;
       }

       if (jumping) { 
            verticalSpeed += .0099f * delta;//change this value to alter gravity strength          
       }

       if(PosY > 300){         
           jumping = false;
           verticalSpeed = 0;          
           PosY = 300;
       }

       PosY += verticalSpeed;



        if(health == 0){
            CharX = 0;
            health = 1000;
        }


    }

    public int getID(){
        return 2;
    }
}
4

3 回答 3

0

您需要按增量缩放跳跃速度。现在你只是在否定 delta,这意味着你的跳跃能力等于每毫秒帧之间经过的时间。这太变态了。

您的代码实际上应该是这样的:

float JUMP_POWER = .1f;
if(input.isKeyPressed(Input.KEY_W) && !jumping){
    verticalSpeed = -delta*JUMP_POWER;
}

这意味着您的角色在跳跃时的垂直速度为每毫秒 0.1 像素。如果增量以毫秒为单位:) 不确定是否是。如果看不到角色移动的位置,就增加跳跃力。

您这样做是因为每个动作都需要按时间缩放。简单的物理学在现实生活中是如何工作的。速度随时间改变位置。因此,为了模拟这一点,您需要计算相对于时间步长之间时间的速度。

于 2013-07-09T23:57:30.103 回答
0

如果 delta 是某种“经过时间”的值,那么你不应该乘以它。跳跃速度与您的帧速率无关。只要选择一个好的速度速度:

if (input.isKeyPressed(Input.KEY_W) && !jumping)
{
    verticalSpeed = -1.0f;
    jumping = true;
}

玩一下这个值。

于 2013-07-10T00:03:04.030 回答
0

你实际上错过了最后一块拼图,如果你回想一下物理课,以恒定加速度改变位置实际上是:

d = (v * t) + (1/2 * a * t^2)

其中 d 是距离 v 是速度 t 是时间 a 是加速度

就像是...

if (input.isKeyPressed(Input.KEY_W) && !jumping) { 
        verticalSpeed = JUMP_POWER;
        jumping = true;
}
PosY = PosY + (verticalSpeed * deltaTime) + (0.5f * gravity * deltaTime * deltaTime);
verticalSpeed = gravity * deltaTime;

因此,您不仅需要将速度乘以时间,而且要使游戏在任何 FPS 上运行相同,您还需要添加 1/2 * a * t^2 来计算正确的距离。

于 2013-09-18T03:31:23.467 回答