2015-10-19 21 views
0

我想要訪問我的精靈表中的特定行,我對如何做到這一點感到非常困惑。我嘗試修改我發現要完成的教程,但是當我運行代碼時,當我的spritesheet和12列中有五行時,我的動畫數組長度爲0。我的課應該總是用一行信息填充我的動畫數組。這裏是我Animator在spritesheet中獲取特定行libgdx java

public Animator(Texture spriteSheet, int cols){ 
    this.walkSheet = spriteSheet; 
    this.cols = cols; 
    this.rows = 1; 
} 

public void create() { 
     TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth()/cols, walkSheet.getHeight()/rows);    // #10 
     walkFrames = new TextureRegion[cols]; 
     anim = new Animation[rows]; 
     for (int i = 0; i < rows; i++) { 
     for (int j = 0; j < cols; j++) { 
      walkFrames[j] = tmp[i][j]; 
     } 
     anim[i] = new Animation(0.025f, walkFrames); 
     } 
     stateTime = 0f;       // #13 
    } 

public void render(int index, SpriteBatch spriteBatch, int x, int y, int width, int height) { 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); 
    stateTime += Gdx.graphics.getDeltaTime(); 
    currentFrame = anim[index].getKeyFrame(stateTime, true); 
    spriteBatch.draw(currentFrame, x, y, width, height); 
} 

任何幫助,這將是非常讚賞,因爲我花了這麼多時間試圖弄清楚這一點。

+0

您指定的行數= 1,並且您有5行是正確的?請問爲什麼? –

回答

1

我還沒有試過這個代碼,但我認爲這可以爲你想要的。

public Animator(Texture spriteSheet, int cols, int rows){ 
    this.walkSheet = spriteSheet; 
    this.cols = cols; 
    this.rows = rows; 
} 

public void create() { 
     TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth()/cols, walkSheet.getHeight()/rows);    // #10 
     anim = new Animation[rows]; 
     for (int i = 0; i < rows; i++) { 
     walkFrames = new TextureRegion[cols]; 
     for (int j = 0; j < cols; j++) { 
      walkFrames[j] = tmp[i][j]; 
     } 
     anim[i] = new Animation(0.025f, walkFrames); 
     } 
     stateTime = 0f;       // #13 
    } 

public void render(int index, SpriteBatch spriteBatch, int x, int y, int width, int height) { 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); 
    stateTime += Gdx.graphics.getDeltaTime(); 
    currentFrame = anim[index].getKeyFrame(stateTime, true); 
    spriteBatch.draw(currentFrame, x, y, width, height); 
} 
+0

非常感謝你!你是我的英雄 ! – Luke

+0

不客氣。 :) –

+1

爲什麼TextureRegion必須在每個循環中實例化? Animation類爲構造函數中的每個框架創建一個新的TextureRegion。 –