libgdx and android application. Image as background

走远了吗. 提交于 2019-12-04 23:08:34

问题


I am new and I just start my journey with libgdx. I would like to know how I can do image.png in resolution 960x640 as background in my game? This is possible? Thx for advices and forbearance. Maybe you hava a simply tutorial? This is my render class:

public void render() {

     texture = new Texture(Gdx.files.internal("E:/background.png"));

     Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
     batch.begin();
     batch.draw(texture, 0, 0);
     batch.end();
}

Second question. I need to insert two active images, active means when I click on that image, the next image show me on the screen. I want to implement action when I click on that picture.


回答1:


In your create() method, create a new Texture referencing your image.png, and then use your existing SpriteBatch to render it in the render() loop. Immediately after your GL.clear() call, go your batch.draw(backgroundTexture, 0. 0) and make sure you're in OrthographicProjection mode for your camera.




回答2:


first you have to set the view port do this in your create method

`float scrw = 960; float scrh = 640;

    camera = new OrthographicCamera();
    camera.viewportHeight = scrh;
    camera.viewportWidth = scrw;

    camera.position.set(camera.viewportWidth * .5f,
            camera.viewportHeight * .5f, 0f);
    camera.update();`

create a texture

texture = new Texture("data/background.png");

put this texture in a sprite like this

sprite=new sprite(texture);

and then set the size like this

sprite.setsize(960,640);

and draw it in your render methods between batch.begin and batch.end

sprite.draw(batch);



来源:https://stackoverflow.com/questions/9384662/libgdx-and-android-application-image-as-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!