2017-06-15 101 views
1

我目前擁有我的主要人物「學生」的類,它除了左右移動之外沒有行爲。我設法做到了,所以我的Spritesheet的所有框架都會渲染,所以當我按下LEFT/RIGHT鍵時,我需要幫助繪製前3幀(這是漫步週期)。 這裏是spritesheet:http://imgur.com/a/HHdm9使用TextureRegion在Java中繪製部分精靈(LibGDX)

編輯:和第二和第三行按下向上和向下鍵時。

學生班級

public class Student { 

    private Texture player; 
    private Animation<TextureRegion> playerAnimation; 
    private int pX; 
    private int pY; 
    private SpriteBatch batch; 
    private int HP; 

    public Student(float speed, int x, int y){ 
     player = new Texture("student.png"); 
     TextureRegion[][] tmp= TextureRegion.split(player,450/3,450/3); 
     TextureRegion[] character = new TextureRegion[9]; 
     int index = 0; 
     for(int i=0; i <3; i++){ 
      for(int j=0; j < 3; j++){ 
       character[index++] = tmp[i][j];} 
     } 

     playerAnimation = new Animation<TextureRegion>(speed,character); 
     pX=x; 
     pY=y; 
    } 

    public SpriteBatch getBatch() { 
     return batch; 
    } 

    public void setBatch(SpriteBatch batch) { 
     this.batch = batch; 
    } 

    public void goLeft(){ 
     pX-=5; 
     if(pX < -10){ 
      pX = -10; 
     } 
    } 

    public void goRight(){ 
     pX+=5; 
    } 

    public void renderStudent(float stateTime){ 
     TextureRegion currentFrame = playerAnimation.getKeyFrame(stateTime,true); 
     batch.draw(currentFrame, (float) pX, (float) pY, 0, 0, currentFrame.getRegionWidth(), 
       currentFrame.getRegionHeight(),1.5f,1.5f,0); 
    } 

    public void dispose(){ 
     player.dispose(); 
    } 
} 

主類

public class HaxMain extends ApplicationAdapter { 
    SpriteBatch batch; 
    Texture bg;         

    float stateTime; 

    private Student student1; 
    private Guard mguard, fguard; 
    private Admin madmin, fadmin; 

    @Override 
    public void create() { 
     batch = new SpriteBatch();       
     bg = new Texture("Level1.png"); 

     student1 = new Student(.5f, 100, 0); 
     student1.setBatch(batch); 
    } 

    @Override 
    public void render() { 
     Gdx.gl.glClearColor(1, 0, 0, 1);  
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);      

     stateTime += Gdx.graphics.getDeltaTime(); 

     if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){ 
      student1.goLeft(); 
      //Code to render image to the left 
     }else if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){ 
      //Code to render image to the right 
      student1.goRight(); 
     } 

     batch.begin();             /*default code*/ 

     batch.draw(bg, 0, 0); 
     student1.renderStudent(stateTime); 
     batch.end();             
    } 

    @Override 
    public void dispose() { 
     batch.dispose(); 
     bg.dispose(); 
     student1.dispose(); 
    } 
} 
+0

'student.png'文件裏面有多少幀,3幀是運行動作,其餘的都是? – Aryan

+0

@AbhishekAryan他們都是3x3,最上面是行走的循環。第二排和第三排用於進入或退出房間,順便說一句,我的遊戲的sidescroller,角色可以進入一個房間,當敵人經過時隱藏。這裏有一個圖像http://imgur.com/a/HHdm9 –

回答

0

您僅需在運行時創建動畫,並使用動畫來展示你的運行播放器向左或向右。你可以根據你這樣的球員的方向翻轉TextureRegion:

TextureRegion[] character; 
boolean isRight;  

public Student(float speed, int x, int y){ 
    player = new Texture("student.png"); 
    TextureRegion[][] tmp= TextureRegion.split(player,450/3,450/3); 
    character = new TextureRegion[9]; 
    int index = 0; 
    for(int i=0; i <3; i++){ 
     for(int j=0; j < 3; j++){ 
      character[index++] = tmp[i][j];} 
    } 

    TextureRegion runningFrames[]=new TextureRegion[3]; 

    for (int i=0;i<runningFrames.length;i++) 
     runningFrames[i]= character[i]; 

    playerAnimation = new Animation<TextureRegion>(speed, runningFrames); 
    playerAnimation.setPlayMode(Animation.PlayMode.LOOP); 
    pX=x; 
    pY=y; 

} 

public void goLeft(){ 
    pX-=5; 
    if(pX < -10){ 
     pX = -10; 
    } 
    if(isRight){ 
     isRight=false; 
     for (TextureRegion textureRegion:playerAnimation.getKeyFrames()) 
      if(textureRegion.isFlipX()) textureRegion.flip(true,false); 
    } 
} 

public void goRight(){ 
    pX+=5; 
    if(!isRight){ 
     isRight=true; 
     for (TextureRegion textureRegion: playerAnimation.getKeyFrames()) 
      if(!textureRegion.isFlipX()) textureRegion.flip(true,false); 
    } 
} 

創建的其他動作與另一動畫和標誌使用繪製動畫的幀。

+0

好吧,我認爲我在這裏取得了進展,我是新的libgdx btw。我在playerAnimation = new Animation (speed,runningFrames,Animation.PlayMode.LOOP)中收到錯誤;它說它無法解析Animation的構造函數(float,com.badlogic.gdx.graphics.g2d.TextureRegion [],com.badlogic.gdx.graphics.g2d.Animation.PlayMode)。它是否與我在Student構造函數中的參數有關? –

+0

@ErrolEmpeño請檢查,我已更新我的答案 – Aryan

+0

我剛剛檢查了您的更新,它的工作原理!所以對於其他兩種行爲(進入/退出房間),基本上我只需創建新的動畫,是否必須分開這些幀並創建他們自己的Spritesheet,或者在分割TextureRegion之後是否可以使用相同的幀? –