简体   繁体   English

java slick2D模拟重力跳跃

[英]java slick2D simulate gravity for jumping

So I made a little game in java with slick2D, but I want my character to jump. 所以我用slick2D在java中制作了一个小游戏,但我想让我的角色跳起来。 Now I have this: 现在我有这个:

if (container.getInput().isKeyPressed(Input.KEY_UP) && !jumping) { 
             verticalSpeed = -1.0f * delta;//negative value indicates an upward movement 
             jumping = true;

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

        } 

        playerY += verticalSpeed;
        playerPoly.setY(playerY);

Well that makes my sprite jump and fall down...for ever. 好吧,这让我的精灵跳了起来......永远。 So how can you make it stop from falling when it hits the ground? 那么你怎么能让它在撞到地面时停下来呢? I tried this: 我试过这个:

if (entityCollisionWith()){
jumping = false;
}

But it doesn't work :( 但它不起作用:(

my code with jumping: 我的跳码:

import org.newdawn.slick.Animation;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;    
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.geom.Polygon;
import org.newdawn.slick.tiled.TiledMap;


public class Game extends BasicGame {

public BlockMap map;
Image player = null;
float playerX = 320;
float playerY = 288;
float scale = 1f;
private Polygon playerPoly;
 boolean jumping = false; 
 float verticalSpeed = 0.0f;


public Game() {
    super("my game");
}
public void init(GameContainer container) throws SlickException {
         map = new BlockMap("data/Map01.TMX");
         player = new Image("data/rightSprite.png");

         playerPoly = new Polygon(new float[]{
                playerX,playerY,
                playerX+17,playerY,
                playerX+17,playerY+48,
                playerX,playerY+48
        });

}        
    public void update(GameContainer container, int delta) throws SlickException {
        if (container.getInput().isKeyDown(Input.KEY_LEFT)) {
            playerX-=0.15;
            playerPoly.setX(playerX);
            if (entityCollisionWith()){
                playerX+=0.15;
                playerPoly.setX(playerX);
            }
        }
        if (container.getInput().isKeyDown(Input.KEY_RIGHT)) {

            playerX+=0.15;
            playerPoly.setX(playerX);
            if (entityCollisionWith()){
                playerX-=0.15;
                playerPoly.setX(playerX);
            }
        }
        if (container.getInput().isKeyPressed(Input.KEY_UP) && !jumping) { 
             verticalSpeed = -1.0f * delta;//negative value indicates an upward movement 
             jumping = true;

        }
        if (entityCollisionWith()){
            jumping = false;
            verticalSpeed = -1.1f;
            verticalSpeed = 0;
        }

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

        } 

        playerY += verticalSpeed;
        playerPoly.setY(playerY);

        if (entityCollisionWith()){
            jumping = false;
            verticalSpeed = 0;
        }

    }



    public boolean entityCollisionWith() throws SlickException {
        for (int i = 0; i < BlockMap.entities.size(); i++) {
            Block entity1 = (Block) BlockMap.entities.get(i);
            if (playerPoly.intersects(entity1.poly)) {
                return true;
            }       
        }       
        return false;
    }


public void render(GameContainer container, Graphics g)  {
    BlockMap.tmap.render(0, 0);
    player.draw(playerX, playerY, scale);
    g.draw(playerPoly);
}
public static void main(String[] argv) throws SlickException {

    AppGameContainer container = new AppGameContainer(new Game(), 640, 480,         false);
        container.start();
}
    }

You should also reset the speed variable after stop jumping: 您还应该在停止跳转后重置速度变量:

if (entityCollisionWith()){
    jumping = false;
    verticalSpeed = 0;
}

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

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