gpt4 book ai didi

java - 在 libGDX 中平滑跳转

转载 作者:行者123 更新时间:2023-11-30 01:56:54 28 4
gpt4 key购买 nike

我想为我的播放器创建一个平滑的跳跃,它先上升然后下降它是一款像马里奥一样的 2D 横向卷轴游戏

我尝试过等待并通过使用很多步骤来减慢跳跃速度,但我无法弄清楚

玩家控制类:

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 像素,开始时快,然后慢下来

最佳答案

我认为你需要的是一个用于游戏的2D物理引擎,最流行的是Box2D

有很多关于这方面的教程,例如 Brent Aureli 的 works ,要跳跃你的角色,你只需对其施加力量,就像

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

希望这有帮助

关于java - 在 libGDX 中平滑跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54137433/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com