2015-09-27 66 views
0

我想並排渲染兩個紋理。然後將這兩種紋理的組合看作一個精靈來使用單個相機。我能夠使用SpriteBatch並排渲染兩個紋理。我如何將這兩個紋理組合成一個精靈並使用相機來獲得不同的視圖?libgdx - 使用SpriteBatch並使用一個攝像頭並排渲染兩個紋理

當我用SpriteBatch繪製兩個紋理後,我試圖用sprite繪製這樣的SpriteBatch。但是在調用sprite.draw()時我得到了空指針。

我的最終目標是並排渲染兩幅圖像作爲背景,並且能夠在我的背景上使用相機。有任何想法嗎? THX

我當前的代碼:

public void create() { 
    batch = new SpriteBatch(); 
    //create two texture for 1.jpg and 2.jpg 
    //use batch to draw them separately at different position 
    img_1 = new Texture("1.jpg"); 
    img_2 = new Texture("2.jpg"); 

    mWorld = new Sprite(); 
    mWorld.setPosition(0, 0); 
    mWorld.setSize(WORLD_WIDTH,WORLD_HEIGHT); 
    float aspectRatio = (float)Gdx.graphics.getHeight()/(float)Gdx.graphics.getWidth(); 
    camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_WIDTH * aspectRatio); 
    camera.position.set(camera.viewportWidth/2f, camera.viewportHeight/2f, 0); 
    camera.update(); 
} 

@Override 
public void render() { 
    batch.setProjectionMatrix(camera.combined); 
    batch.begin(); 
    batch.draw(this.img_1, 0, 0, WORLD_WIDTH/2, WORLD_HEIGHT); 
    batch.draw(this.img_2, WORLD_WIDTH/2, 0, WORLD_WIDTH/2, WORLD_HEIGHT); 

    //getting java nullpointer exception here 
    mWorld.draw(batch); 
    batch.end(); 
} 
+1

有你設定精靈mWorld紋理?我沒有看到任何東西,也發佈了完整的stackTrace – SteveL

回答

0

雪碧是一個簡單的包裝爲一個單一的紋理(或TextureRegion)(添加更多的功能)。

你只需調用「new Sprite();」它會創建一個沒有紋理的精靈(因此沒有繪製任何東西,沒有圖像信息等),因此當您嘗試繪製精靈時,會得到空指針異常。

既然你想要結合兩個紋理,爲什麼不用一個外部工具(如pain.net或gimp,..)創建一個紋理(圖像)呢?

如果你想繪製兩個紋理作爲一個對象,你需要創建自己的類,例如:

import com.badlogic.gdx.ApplicationAdapter; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.Batch; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 

public class TestGame extends ApplicationAdapter 
{ 

    public class TwoTextures 
    { 
     private final Texture tex1, tex2; 
     private final float  pos1X, pos1Y, pos2X; 

     public TwoTextures(Texture tex1, Texture tex2, float posX, float posY) 
     { 
      this.tex1 = tex1; 
      this.tex2 = tex2; 
      this.pos1X = posX; 
      this.pos1Y = posY; 

      this.pos2X = posX + tex1.getWidth(); 
     } 

     public void draw(Batch batch) 
     { 
      batch.draw(tex1, pos1X, pos1Y); 
      batch.draw(tex2, pos2X, pos1Y); 
     } 

     public void dispose() 
     { 
      tex1.dispose(); 
      tex2.dispose(); 
     } 
    } 

    OrthographicCamera camera; 

    Batch    batch; 

    TwoTextures   background; 

    @Override 
    public void create() 
    { 
     batch = new SpriteBatch(); 
     camera = new OrthographicCamera(); 

     background = new TwoTextures(
       new Texture("1.jpg"), 
       new Texture("2.jpg"), 
       0, 
       0); 
    } 

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

     batch.begin(); 
     { 
      background.draw(batch); 
     } 
     batch.end(); 
    } 

    @Override 
    public void dispose() 
    { 
     batch.dispose(); 
     background.dispose(); 
    } 
}