简体   繁体   中英

Sprite class not drawn when using Sprite.draw(batch) LibGDX

I'm trying to show a texture scaled using the Sprite class. The issue is when I try to run my code. I can show the textures in Android 4.1, but when I run the same program in another Android, for example 2.2, 2.3.6... the Sprite is not shown. Here's my code.

public void render() {
    // TODO Auto-generated method stub
    Gdx.gl10.glClearColor(1, 0.4f, 0.3f, 1);
    Gdx.gl10.glClear(GL10.GL_COLOR_BUFFER_BIT);
    if(estadojuego==0){
        batch.begin();
        sboton1.draw(batch);
        sboton2.draw(batch);
        batch.end();
        if(Gdx.input.isTouched()){
            System.out.println("x"+Gdx.input.getX());
            System.out.println("y"+Gdx.input.getY());
            if(Gdx.input.getY()<(altura-posicion1.y) && Gdx.input.getY()>(altura-(posicion1.y+posicion1.height))){
                if(Gdx.input.getX()<(posicion1.x+posicion1.width) && Gdx.input.getX()>posicion1.x){
                    //estadojuego=1;
                    System.out.println("boton1");
                }
                if(Gdx.input.getX()<(posicion2.x+posicion2.width) && Gdx.input.getX()>posicion2.x){
                    estadojuego=2;
                }
            }
        }
    }
    if(estadojuego==2){
        batch.begin();
        sboton3.draw(batch);
        batch.end();
        if(Gdx.input.isTouched()){
            if(Gdx.input.getY()<(altura-posicion3.y) && Gdx.input.getY()>(altura-(posicion3.y+posicion3.height))){
                if(Gdx.input.getX()<(posicion3.x+posicion3.width) && Gdx.input.getX()>posicion3.x){
                    estadojuego=0;
                }
            }
        }
    }
}

Thanks a lot and sorry for my bad english.

It seems like you didn't set the Matrix4 for your batch.

Use this :

private Matrix4 Projection;

In create() :

Projection = new Matrix4().setToOrtho2D(0, 0, (float) Gdx.graphics.getWidth(), (float) Gdx.graphics.getHeight());

In render() before batch.begin();

batch.setProjectionMatrix(Projection);

I hope this 'll help you.

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