简体   繁体   中英

Sprite doesn't display in Libgdx

I make simply aplication in LibGDX. I have got two textures (background and sprite). The background is diplayed ok but I don't see the sprite. I have tried many solutions but nothing works. I don't know what is going on. Thanks for help.

Here is my code:

public void create() {      
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
    Texture.setEnforcePotImages(false);
    camera = new OrthographicCamera(1, h/w);
    batch = new SpriteBatch();
    texture = new Texture(Gdx.files.internal("graphic/bg1.png"));
    bad = new Texture(Gdx.files.internal("graphic/b.png"));
    layerOne = new TextureRegion(texture);
    spriteb = new Sprite(bad);
    rbg = new ParallaxBackground(new ParallaxLayer[]{
            new ParallaxLayer(layerOne, new Vector2(),new Vector2()),
            }, w, h,new Vector2());
}

public void render() {      
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    rbg.render();
    camera.update();
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    spriteb.setPosition(400, 200);
    spriteb.draw(batch);
    batch.end();
}

Your camera viewport is too small for the Sprite position

camera = new OrthographicCamera(1, h/w);
...
spriteb.setPosition(400, 200);

The sprite is being rendered far outside of the viewport.

Try setting your camera like this:

camera = new OrthographicCamera(w, (h/w)*h);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM