java - libgdx 为什么相机不跟随角色?

标签 java android libgdx

我是 libgdx 的新手,我已经阅读了教程(教程代码是我的基础)。 我有一个可以移动的角色,但相机不会跟随角色:(

这是我的 WorldRenderer.java:

package com.evolutio.tee.view;

import com.evolutio.tee.model.Block;
import com.evolutio.tee.model.Tee;
import com.evolutio.tee.model.World;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;

public class WorldRenderer {

    private static final float CAMERA_WIDTH = 10f;
    private static final float CAMERA_HEIGHT = 7f;

    private World world;
    private OrthographicCamera cam;

    /** for debug rendering **/
    ShapeRenderer debugRenderer = new ShapeRenderer();

    /** Textures **/
    private Texture teeTexture;
    private Texture blockTexture;

    private SpriteBatch spriteBatch;
    private boolean debug = false;
    private int width;
    private int height;
    private float ppuX; // pixels per unit on the X axis
    private float ppuY; // pixels per unit on the Y axis

    public void setSize (int w, int h) {
        this.width = w;
        this.height = h;
        ppuX = (float)width / CAMERA_WIDTH;
        ppuY = (float)height / CAMERA_HEIGHT;
    }

    public WorldRenderer(World world, boolean debug) {
        Tee tee = world.getTee();
        this.world = world;
        cam = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
        cam.position.set(CAMERA_WIDTH / 2f, CAMERA_HEIGHT / 2f, 0);
        cam.update();
        this.debug = debug;
        spriteBatch = new SpriteBatch();
        loadTextures();
    }

    private void loadTextures() {
        teeTexture = new  Texture(Gdx.files.internal("tee/tee.png"));
        blockTexture = new Texture(Gdx.files.internal("world/dreck.png"));
    }

    public void render() {
        spriteBatch.begin();
            drawBlocks();
            drawTee();
        spriteBatch.end();
        //if (debug) drawDebug();
    }


    private void drawBlocks() {
        for (Block block : world.getBlocks()) {
            spriteBatch.draw(blockTexture, block.getPosition().x * ppuX, block.getPosition().y * ppuY, Block.SIZE * ppuX, Block.SIZE * ppuY);
        }
    }

    private void drawTee() {
        Tee tee = world.getTee();
        spriteBatch.draw(teeTexture, tee.getPosition().x * ppuX, tee.getPosition().y * ppuY, Tee.SIZE * ppuX, Tee.SIZE * ppuY);
        cam.position.set(tee.getPosition().x, tee.getPosition().y, 0);
    }

    private void drawDebug() {
        // render blocks
        debugRenderer.setProjectionMatrix(cam.combined);
        debugRenderer.begin(ShapeType.Rectangle);
        for (Block block : world.getBlocks()) {
            Rectangle rect = block.getBounds();
            float x1 = block.getPosition().x + rect.x;
            float y1 = block.getPosition().y + rect.y;
            debugRenderer.setColor(new Color(1, 0, 0, 1));
            debugRenderer.rect(x1, y1, rect.width, rect.height);
        }
        // render Tee
        Tee tee = world.getTee();
        Rectangle rect = tee.getBounds();
        float x1 = tee.getPosition().x + rect.x;
        float y1 = tee.getPosition().y + rect.y;
        debugRenderer.setColor(new Color(0, 1, 0, 1));
        debugRenderer.rect(x1, y1, rect.width, rect.height);
        debugRenderer.end();
    }
}

在 drawTee() 中,我尝试让相机跟随 T 恤。

cam.position.set(tee.getPosition().x, tee.getPosition().y, 0);

最佳答案

试试这个

public class WorldRenderer {

private static final float CAMERA_WIDTH = 10f;
private static final float CAMERA_HEIGHT = 7f;

private World world;
private OrthographicCamera cam;

/** for debug rendering **/
ShapeRenderer debugRenderer = new ShapeRenderer();

/** Textures **/
private Texture teeTexture;
private Texture blockTexture;

private SpriteBatch spriteBatch;
private boolean debug = false;
private int width;
private int height;
private float ppuX; // pixels per unit on the X axis
private float ppuY; // pixels per unit on the Y axis

public void setSize (int w, int h) {
    this.width = w;
    this.height = h;
    ppuX = (float)width / CAMERA_WIDTH;
    ppuY = (float)height / CAMERA_HEIGHT;
}

public WorldRenderer(World world, boolean debug) {
    Tee tee = world.getTee();
    this.world = world;
    this.cam = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
    this.cam.setToOrtho(false,CAMERA_WIDTH,CAMERA_HEIGHT);
    this.cam.position.set(CAMERA_WIDTH / 2f, CAMERA_HEIGHT / 2f, 0);
    this.cam.update();
    this.debug = debug;
    spriteBatch = new SpriteBatch();
    loadTextures();
}

private void loadTextures() {
    teeTexture = new  Texture(Gdx.files.internal("tee/tee.png"));
    blockTexture = new Texture(Gdx.files.internal("world/dreck.png"));
}

public void render() {
    moveCamera(tee.getPosition().x, CAMERA_HEIGHT / 2);
    spriteBatch.setProjectionMatrix(cam.combined);
    spriteBatch.begin();
        drawBlocks();
        drawTee();
    spriteBatch.end();
    //if (debug) drawDebug();
}

public void moveCamera(float x,float y){
    if ((tee.getPosition().x > CAMERA_WIDTH / 2)) {
        cam.position.set(x, y, 0);
        cam.update();
    }

}


private void drawBlocks() {
    for (Block block : world.getBlocks()) {
        spriteBatch.draw(blockTexture, block.getPosition().x, block.getPosition().y, Block.SIZE, Block.SIZE);
    }
}

private void drawTee() {
    Tee tee = world.getTee();
    spriteBatch.draw(teeTexture, tee.getPosition().x, tee.getPosition().y, Tee.SIZE, Tee.SIZE);
}

在需要 spriteBatch.begin() 之前使用 sprite.setProjectionMatrix(cam.combined) 是因为如果您不这样做,spriteBatch 将使用它自己的摄像头,因此您的更新不会对屏幕上显示的内容执行任何操作。 你必须去掉 ppuX,ppuY 的东西。我知道这是为不同的设备调整屏幕大小,但它会搞砸相机,因此如果你把它放在里面并调用 setProjectionMatrix,它会被放大(实际上所有图像的尺寸都会很大。)

关于java - libgdx 为什么相机不跟随角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14629653/

相关文章:

java - 重新启动 TCP 服务器

Android EditText 显示带有大写字母和数字行的键盘

java - 为什么 Libgdx Bullet 中的 ContactListener 派生类中的数组大小始终为零?

LibGdx Stage/Actor InputListener(InputListener 的适当区域)

java - 如何使用集合的 addall() 方法?

java - 兔子MQ : Connection recovery mechanism

php - 我如何在 html 中显示 pdf(响应/跨浏览器解决方案)

java - libgdx 如何缩放 BitmapFont 以适应不断变化的屏幕尺寸?

java - 在 Java Spring 应用程序中找不到包

java - 如何创建一个 "extends"两个现有具体类的类