2017-01-11 29 views
2

我有一個精靈圖像的紋理,看起來像這樣:Kirby RunlibGDX動畫效果紋理區域

而且我想製作動畫。我有這樣的代碼設置到每個幀存儲到一個變量TextureRegion然後存儲所有的幀轉換爲動畫TextureRegion陣列

//AssetLoader class 
runTexture = new Texture("kirbyrun.png"); 
runTexture.setFilter(TextureFilter.Nearest,TextureFilter.Nearest); 

//Run Hiro 
hiro1 = new TextureRegion(runTexture,0,55,37,55); 
hiro1.flip(false,true); 
hiro2 = new TextureRegion(runTexture,37,55,44,55); 
hiro2.flip(false,true); 
hiro3 = new TextureRegion(runTexture,81,55,44,55); 
hiro3.flip(false,true); 
hiro4 = new TextureRegion(runTexture,129,55,46,55); 
hiro4.flip(false,true); 
hiro5 = new TextureRegion(runTexture,176,55,41,55); 
hiro5.flip(false,true); 
hiro6 = new TextureRegion(runTexture,216,55,41,55); 
hiro6.flip(false,true); 
hiro7 = new TextureRegion(runTexture,257,55,41,55); 
hiro7.flip(false,true); 
hiro8 = new TextureRegion(runTexture,301,55,42,55); 
hiro8.flip(false,true); 

TextureRegion[] run = {hiro1,hiro2,hiro3,hiro4,hiro5,hiro6,hiro7,hiro8}; 
hiroRunAnimation = new Animation<TextureRegion>(0.5f,run); 
hiroRunAnimation.setPlayMode(Animation.PlayMode.LOOP); 

我當前使用yDown座標系這就是爲什麼我翻轉圖像。

TextureRegion currentFrame = AssetLoader.hiroRunAnimation.getKeyFrame(runTime); 

sb.begin(); 

sb.draw(currentFrame,player.getX(),player.getY(),player.getWidth(),player.getHeight()); 
sb.end(); 

這只是塊而不是科比的圖片打開:

試圖呈現這一點使用此代碼後 enter image description here

很明顯我做錯了什麼,我米猜測它與我的TextureRegion部分,也許我的X,Y座標或錯誤,但我不知道如何解決此問題,因爲我使用spritecow.com獲取這些座標。我假設左上角是(0,0),正Y意味着向下移動圖像。我怎樣才能使柯比實際出現而不是這些盒子?或者更容易將每個幀圖像分離成單獨的.png文件,只需使用Texture而不是TextureRegion,因此我不需要指定明確的區域。那麼我可以使用動畫或不會工作?

+0

你的spritesheet上有那些黑盒子嗎? – eldo

回答

1

黑盒子是因爲你錯誤地定義了區域位置。你從y = 55開始所有區域,因爲紋理從y = 0開始,這是不正確的。 你也翻轉紋理,這是使它們顛倒顯示,所以刪除翻轉以及。 您似乎也錯誤地確定了某些區域的區域大小,因此您應該仔細檢查它們。

您的代碼應該是這樣的:

// Note the third value have changed to zeroes and the flipping has been removed. 
hiro1 = new TextureRegion(runTexture,0,0,37,55); 
hiro2 = new TextureRegion(runTexture,37,0,44,55); 
hiro3 = new TextureRegion(runTexture,81,0,44,55); 
hiro4 = new TextureRegion(runTexture,129,0,46,55); 
hiro5 = new TextureRegion(runTexture,176,0,41,55); 
hiro6 = new TextureRegion(runTexture,216,0,41,55); 
hiro7 = new TextureRegion(runTexture,257,0,41,55); 
hiro8 = new TextureRegion(runTexture,301,0,42,55); 

這樣做的結果之後是這樣的:

enter image description here

+0

你怎麼能得到每個精靈的確切座標? –

+0

我沒有改變座標(x,寬度,高度),我只是改變了開始的y位置。 – Charanor

3

我是看它的權利,你「硬編碼」的協調了精靈?這不是一個好主意,libGDX有一個更好的解決方法:Texture Packer

基本上,你要做的是將單個精靈放到一個文件夾中運行紋理打包器腳本來合併它們。你還將得到一個帶有精靈座標的json文件。您可以使用資產管理器輕鬆加載它們。我在這裏創建了一個例子:https://gist.github.com/reime005/87e1ed30548be31a632292a604f397ef