Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
libgdx-使用SpriteBatch并排渲染两个纹理,并使用一个摄影机_Libgdx_Textures_Sprite_Spritebatch - Fatal编程技术网

libgdx-使用SpriteBatch并排渲染两个纹理,并使用一个摄影机

libgdx-使用SpriteBatch并排渲染两个纹理,并使用一个摄影机,libgdx,textures,sprite,spritebatch,Libgdx,Textures,Sprite,Spritebatch,我想同时渲染两个纹理。然后考虑将这两种纹理组合为一个精灵使用单个相机。我能够使用SpriteBatch并排渲染两个纹理。如何将这两种纹理组合成一个精灵,并使用摄影机获得不同的视图 目前,在我用SpriteBatch绘制了两个纹理之后,我正在尝试用sprite绘制这样的SpriteBatch。但是调用sprite.draw()时,我得到了空指针 我的最终目标是并排渲染两幅图像作为背景,并且能够在我的背景上使用相机。有什么想法吗?THX 我当前的代码: public void create () {

我想同时渲染两个纹理。然后考虑将这两种纹理组合为一个精灵使用单个相机。我能够使用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();
}

Sprite只是单个纹理(或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();
    }
}

你为精灵世界设置了纹理吗?我什么也没看到,也发了完整的stackTrace