2013-02-05 106 views
0

我试图在由libgdx库构建的java应用程序上加载两个图像。然而,我加载了背景图像,如果我没有将图像的位置设置为(0,0),我无法将其他图像加载到屏幕上。例如, ; 我设置图像的位置为0,0,没有问题。但是,当我将图像的位置设置为20,0时,无法看到。使用libgdx加载图像

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

我想用上面的代码画出图像。

谢谢。

编辑:

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); 

回答

2

的TextureRegion类描述矩形的纹理内,是用于绘制仅纹理的部分是有用的。

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(); 

在这里20,20,50,50描述纹理的部分,然后绘制在10,10。通过将Texture和其他参数传递给SpriteBatch可以实现同样的效果,但TextureRegion可以方便地使用一个描述两者的单个对象。

SpriteBatch具有用于绘制纹理区域许多方法

来源:source

,如果你有1然后用几个“区域”的变量。(REGION1 =新... 1和区域2个图像=新的...),否则加载2个文件,并执行相同的文件。

+0

在我的状态下,我有两个单独的图像,其中一个是64x64,另一个是32x32。我创建了两个TextureRegion并尝试绘制它们,就像我在我的问题中写的一样。在0,0的位置上,他们很好,否则不会。编辑:在我的程序中,我试图获取像region = new TextureRegion(texture,0,0,64,64)的图像。 –

+1

你能提供你所有的代码吗? – Aleksandrs

+0

'obstacle = loadTexture(“data/obstacle.png”); \t \t obstacleRegion = new TextureRegion(obstacle,0,0,64,64); world.obstacle.position.x + = 0.001; \t \t batch.draw(Assets.obstacleRegion,world.obstacle.position.x, \t \t \t \t world.obstacle.position.y,1,1);'如果我设置world.obstacle.position.x和position.y为0,0,否则不行。这段代码是否正确。由于这是评论区,我无法发布更多。 –