在libGDX中平稳跳转

时间:2019-01-10 21:52:26

标签: java libgdx

我想为我的播放器创建一个平滑的跳跃,它先升后降 它是2D侧面滚动条,如Mario

我尝试使用许多步骤等待并让跳跃变慢,但我无法弄清楚

玩家控制类:

package com.mygdx.game;

import java.lang.Thread.State;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;


@SuppressWarnings("unused")
public class PlayerController {
    static int speed = 1;
    public static int jump = 0;

    static void keyInput() {
        jump++;
        if (Gdx.input.isKeyPressed(Keys.A)) {
            main.playerX += speed;
            main.backgroundSpriteX += speed;
        }
        if (Gdx.input.isKeyPressed(Keys.D)) {
            main.playerX -= speed;
            main.backgroundSpriteX -= speed;
        }
        if (Gdx.input.isKeyPressed(Keys.W)) {
             //this is where i want to be able to jump
        }
    }  
    static void Controller() {


        main.player = new Sprite(main.texture);
        main.playerX = (main.canvisWidth * 0);
        main.playerY = (main.canvisHeight * 0); //can be 0
    }
}

主类:

package com.mygdx.game;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;


public class main implements ApplicationListener {
    public static final int backgroundSpriteY = 0;
    public static final int backgroundSprite2Y = 0;
    public static int canvisWidth = 800;
    public static int canvisHeight = 480;
    public static int backgroundSpriteX = 0;
    public static Texture texture;
    public static int backgroundSprite2X = -canvisWidth;
    public static Sprite player;
    public static int playerX;
    public static int playerY;
    static SpriteBatch spriteBatch;
    static int Jumpframes = 0;
    private double playerSize = .4;

    public void create() {
        WorldObjects.shapeRender.setAutoShapeType(true);
        spriteBatch = new SpriteBatch();
        texture = new Texture(Gdx.files.internal("imageedit_3_3813241913.png"));
        PlayerController.Controller();
        WorldSetup.start();
        player.setSize((float) (player.getWidth() * playerSize), (float) (player.getHeight() * playerSize));
    }

    public void render() {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        PlayerController.keyInput();
        WorldController.Scroll();
        spriteBatch.begin();
        spriteBatch.draw(WorldSetup.backgroundTexture, backgroundSpriteX, backgroundSpriteY);
        spriteBatch.draw(WorldSetup.backgroundTexture2, backgroundSprite2X, backgroundSprite2Y);
        spriteBatch.draw(texture, playerX, playerY, player.getWidth(), player.getHeight());
        spriteBatch.end();
        WorldSetup.WorldRender();
        //Jumpframes++;
    }


    public void resize(int width, int height) {
    }

    public void pause() {
    }

    public void resume() {
    }

    public void dispose() {
    }

}

我想要像马里奥那样的慢跳,而我似乎无法创造慢/平稳跳

我希望20帧跳高30像素,快速开始并放慢速度

2 个答案:

答案 0 :(得分:1)

您也许可以像这样记录起始跳跃位置...而不是直接使用按钮来更改其位置,而是使用按钮来更改其速度,然后使用其当前位置加上xvelocity和yvelocity来计算下一个位置。当他们按下跳动时,y会增加,而您将每一帧的y减小,直到它们降落到平台上,然后将其设置回0。

答案 1 :(得分:1)

我认为您需要的是2D游戏物理引擎,最受欢迎的是Box2D

关于这方面的许多教程,例如布伦特·奥雷利(Brent Aureli)的works,只要像用力一样对角色施加跳跃即可

player.b2body.applyForceToCenter(0, 80f, true);

希望这会有所帮助