2013-01-31 25 views
3

我是新來的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); 

回答

11

試試這個

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

使用sprite.setProjectio在需要spriteBatch.begin()之前需要nMatrix(cam.combined),因爲如果你沒有使用它自己的相機,那麼你的更新對屏幕上顯示的內容不起作用。 你必須拿出ppuX,ppuY的東西。我知道這是對調整爲不同設備的屏幕,但它搞砸了攝像頭,所以如果你把它和調用setProjectionMatrix,將這樣的方式在放大(實際上所有圖像的大小將是巨大的。)

+0

此外,相機將保持與初始設置相同的高度,如果您嘗試將其設置爲字符Y位置,它將會太低。如果你使用Star Assualt教程作爲基礎,那我肯定是100%。 – AspiretoCode

+0

嘿, 我無法測試:( 我已經銷燬了任何代碼:'( 在工作中的代碼有工作,但在家裏它不工作 http://i.stack。 imgur.com/OXGC2.png – Evolutio

+0

你刪除了所有的代碼嗎?如果您使用dropox,你可以在任何地方獲取代碼。 – AspiretoCode