Load image with libgdx

不羁岁月 提交于 2019-12-20 06:14:19

问题


I am trying to load two images on the java application built by libgdx library. I loaded the background image however, I could not load the other images on to the screen if I am not set the position of the images to (0,0). For example; I set the position of the image to 0,0 and there is no problem. However, when I set the position of the image to 20, 0, it cannot be seen.

batch.draw(Assets.coinRegion, position.x, position.y, 1, 1)

I am trying to draw the image with the above code.

Thank you.

Edit:

obstacle = loadTexture("data/obstacle.png"); 
obstacleRegion = new TextureRegion(obstacle, 0, 0, 64, 64);
world.obstacle.position.x += 0.001; 
batch.draw(Assets.obstacleRegion,
world.obstacle.position.x, world.obstacle.position.y, 1, 1); 

回答1:


The TextureRegion class describes a rectangle inside a texture and is useful for drawing only a portion of the texture.

private TextureRegion region;
...
texture = new Texture(Gdx.files.internal("image.png"));
region = new TextureRegion(texture, 20, 20, 50, 50);
//if you have 2 images in image.png add new region and specify rectangular:
//region2 = new TextureRegion(texture, 70, 0, 100, 100);
...
batch.begin();
batch.draw(region, 10, 10);
batch.end();

Here the 20, 20, 50, 50 describes the portion of the texture, which is then drawn at 10,10. The same can be achieved by passing the Texture and other parameters to SpriteBatch, but TextureRegion makes it convenient to have a single object that describes both.

SpriteBatch has many methods for drawing a texture region

source: source

if you have 2 images in 1 then use several "region" variables.. (region1 = new ... and region2 = new...), otherwise load 2 files and do the same what written in documentation.



来源:https://stackoverflow.com/questions/14708271/load-image-with-libgdx

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