【问题标题】:making a smooth jump in libGDX在 libGDX 中进行平滑跳转
【发布时间】:2019-01-10 21:52:26
【问题描述】:

我想为我的播放器创建一个平滑的跳跃,它先升后降 它是一个像马里奥一样的 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 像素开始快速和减速

【问题讨论】:

  • anything * 0 is 0 只需将其设置为 0 ` main.playerX = (main.canvisWidth * 0); main.playerY = (main.canvisHeight * 0); //可以是0`
  • 我知道它只是为了提醒我以后我将使用canvis宽度和高度的百分比来设置起始玩家x和y
  • 通常用于您想在一处检测印刷机的游戏。然后使用所有这些信息来更新状态。然后重绘。您没有提到当前尝试的跳跃动画或代码当前错误的确切性质。您需要记录一些关于跳跃开始的信息(例如 x、y 或帧数,或者至少在跳跃时计算名声)
  • 我基本上只是希望有人向我展示我的代码的工作版本,以便我可以跳转我更改了我的摘要
  • 似乎玩家和背景的移动方向和速度相同?

标签: java libgdx


【解决方案1】:

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

很多关于这方面的教程,比如 Brent Aureli 的 works,要让你的角色跳跃,你只需对它施加力量就可以了

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

希望对你有帮助

【讨论】:

    【解决方案2】:

    您也许可以摆脱像这样记录开始跳跃位置...而不是使用按钮直接改变他们的位置,使用按钮改变他们的速度,然后使用他们的当前位置加上 xvelocity 和 yvelocity 来计算下一个位置.当他们按下跳跃键时,他们会得到一个增加的 y 并且每一帧你都会减少它,直到他们降落在一个平台上然后将它设置回 0。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 1970-01-01
      • 2014-04-26
      • 2014-07-25
      • 2010-12-10
      • 1970-01-01
      相关资源
      最近更新 更多