libgdx ¿por qué la cámara no sigue el personaje?
Soy nuevo con libgdx y he leído un Tutorial (el Tutorial Code es mi base). Tengo un personaje que me puedo mover, pero la cámara no seguirá el carácter 🙁
Aquí está mi WorldRenderer.java:
- No se puede usar Android NDK con libGDX
- Libgdx dibujar caracteres chinos
- Integración multimedia con LibGDX
- LibGDX movimiento no suave
- Pase ArrayList desde el módulo android a núcleo en libgdx
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(); } }
en drawTee () Trato de hacer que la cámara siga la Tee.
cam.position.set(tee.getPosition().x, tee.getPosition().y, 0);
- Acciones de los actores en libgdx
- Cómo detectar el final de la acción en Libgdx (0.9.7) de android
- ¿Cuál es la forma más sencilla de hacer que la imagen sea táctil en libgdx?
- Libgdx: configurar otra pantalla, pero los botones permanentes de la pantalla antigua están activos
- Cómo hacer LibGDX + Android Studio jar archivo?
- Android GDX Box2D forma de triángulo
- ¿Cómo libgdx detecta la presencia del teclado?
- Proyecto LibGDX HelloWorld se bloquea cuando se ejecuta en el emulador de Android
Prueba esto
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); }
Utilizar el sprite.setProjectionMatrix (cam.combined) antes de que spriteBatch.begin () sea necesario porque si no lo hace el spriteBatch utiliza su propia cámara, por lo tanto sus actualizaciones no hacen nada por lo que aparece en la pantalla. Tienes que sacar el ppuX, ppuY cosas. Sé que es para cambiar el tamaño de la pantalla de los diferentes dispositivos, pero se atornilla a la cámara, por lo tanto, si usted lo mantiene en y llamar a la setProjectionMatrix, será manera de zoom (en realidad el tamaño de todas las imágenes será enorme.