2017-06-15 99 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之后是否可以使用相同的帧? –