2013-02-05 107 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,否則不行。這段代碼是否正確。由於這是評論區,我無法發佈更多。 –