为什么我的播放器在Eclipse和LibGDX中不掉落(重力)?]

问题描述 投票:0回答:1

我正试图让我的玩家摔倒,但他没有。他只是停留在地图上。他不能动。您能帮忙吗?

这里是屏幕的代码。这是我拥有显示,渲染,调整大小,暂停,恢复,隐藏等所有方法的地方

package net.hasanbilal.prisonrevelations.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import net.hasanbilal.prisonrevelations.entities.Player;

public class Play implements Screen {

    private TiledMap map;
    private OrthogonalTiledMapRenderer otmr;
    private OrthographicCamera camera;

    private Player porter;

    @Override
    public void show() {
        map = new TmxMapLoader().load("maps/level1.tmx");

        otmr = new OrthogonalTiledMapRenderer(map);

        camera = new OrthographicCamera();

        porter = new Player(new Sprite(new Texture("img/porter.png")));

    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        otmr.setView(camera);
        otmr.render();

        otmr.getBatch().begin();
        porter.draw(otmr.getBatch());
        otmr.getBatch().end();

    }

    @Override
    public void resize(int width, int height) {
        camera.viewportWidth = width;
        camera.viewportHeight = height;
        camera.update();

    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub

    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }

    @Override
    public void hide() {
        dispose();

    }

    @Override
    public void dispose() {
        map.dispose();
        otmr.dispose();

    }

}

这是播放器的代码

package net.hasanbilal.prisonrevelations.entities;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;

public class Player extends Sprite {

    /** 
     * x and y velocity
     */
    private Vector2 velocity = new Vector2();

    //change 120 to 60*2 if it doesn't work
    private float speed = 60 * 2, gravity = 60 * 1.8f;

    public Player(Sprite s) {
        super(s);

    }

    public void draw(SpriteBatch sb) {
        update(Gdx.graphics.getDeltaTime());
        super.draw(sb);

    }
    private void update(float deltaTime) {
        velocity.y -= gravity * deltaTime; //gravity to player

        if(velocity.y>speed)
            velocity.y = speed;
        else if(velocity.y < speed)
            velocity.y = -speed;

        setX(getX() + velocity.x * deltaTime);
        setY(getY() + velocity.y * deltaTime);

    }

}

此作业即将到期!请帮助!

我正试图让我的玩家摔倒,但他没有。他只是停留在地图上。他不能动。你能帮忙吗?这是屏幕的代码。这是我拥有所有类似...

java libgdx box2d gravity
1个回答
0
投票

没关系的人,我修好了。我要做的就是在porter.update(1);porter.draw(otmr.getBatch());之间添加otmr.getBatch().end();

© www.soinside.com 2019 - 2024. All rights reserved.