简体   繁体   English

在libGDX中平稳跳转

[英]making a smooth jump in libGDX

i want to create a jump of my player that is smooth it goes up then down its a 2D side scroller like Mario 我想为我的播放器创建一个平滑的跳跃,它会先上升然后下降,像Mario这样的2D侧面滚动条

iv'e tried wait and make jumps slow by using a lot of steps and i cant figure it out 我尝试了等待,并通过使用许多步骤来使跳跃变慢,但我无法弄清楚

player control class: 播放器控制类:

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
    }
}

main class: 主类:

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() {
    }

}

i want to have a slow jump like a Mario sort of jump and i cant seem to create a slow/smooth jump 我想要像马里奥那样的慢跳,我似乎无法创造慢/平稳跳

I'd like 20 frame jump going up 30 pixels starting fast and slowing down 我想跳20帧,然后开始快速地变慢30像素

You can maybe get away from recording the start jump position like this... instead of using the buttons to directly alter their position, use the buttons to alter their velocity then use their current position plus xvelocity and yvelocity to calculate next position. 您也许可以像这样记录起始跳转位置...而不是直接使用按钮来更改其位置,而是使用按钮来更改其速度,然后使用其当前位置加上xvelocity和yvelocity来计算下一个位置。 when they press jump they get a increased y and every frame you reduce that until they land on a platform then set it back to 0. 当他们按下跳跃时,它们会增加y,而您将每帧减小y,直到它们降落在平台上,然后将其设置回0。

I think what you need is a 2D Physics Engine for Games, The the popular one is Box2D 我认为您需要的是2D游戏物理引擎,最受欢迎的是Box2D

lots of tutorials on this, like Brent Aureli's works , to jump your character you just apply force on it like 关于这方面的许多教程,例如布伦特·奥雷利(Brent Aureli)的作品 ,您只要对它施加力就可以跳跃角色

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

hope this helps 希望这可以帮助

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

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