Java 在libGDX中进行平滑跳跃
我想给我的球员创造一个平稳的跳跃,先上后下 它是一个像马里奥一样的2D侧边卷轴 我试着等待,用很多步使跳跃变慢,但我想不出来 玩家控制等级:Java 在libGDX中进行平滑跳跃,java,libgdx,Java,Libgdx,我想给我的球员创造一个平稳的跳跃,先上后下 它是一个像马里奥一样的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.b
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像素,开始时速度快,开始时速度慢您可能不需要像这样记录开始跳转的位置。。。与其使用按钮直接改变其位置,不如使用按钮改变其速度,然后使用其当前位置加上xvelocity和yvelocity来计算下一个位置。当他们按下“跳跃”键时,他们会得到一个增加的y,每一帧你都会减少y,直到他们降落到平台上,然后将其设置回0。我认为你需要的是游戏的2D物理引擎,最流行的是 很多关于这方面的教程,比如布伦特·奥雷利的,为了跳跃你的角色,你只需对它施加力,就像
player.b2body.applyForceToCenter(0, 80f, true);
希望这有帮助anything*0是0,只需将其设置为0`main.playerX=(main.canvisWidth*0);main.playerY=(main.canvisHeight*0)//可以是0 `我知道这只是为了提醒我以后什么时候我会使用canvis宽度和高度的百分比来设置首发球员x和Y,通常用于你想在一个位置检测压力的游戏。然后使用所有这些信息来更新状态。然后重新绘制。您没有提到当前跳转动画或当前尝试的代码出现问题的确切性质。你会想记录一些关于跳转开始的信息(比如x,y,或者帧数,或者至少在跳转时数名)我基本上只是想有人给我看一个我的代码的工作版本,这样我就可以跳转了。我改变了我的总结。看起来可能播放器和背景在同一个方向和速度上移动?