2014-12-07 133 views
0

這是我第一次來這裏,我有一個問題給你。我希望你能幫助我。因此,我在這場比賽中有這個課程,我希望球員在走路時重複動畫。對不起,如果你不明白我,但我的英語是基本的。這是班級。Libgdx紋理動畫

public class Player extends Actor implements InputProcessor { 

    private final Texture tile4; 
    private final Texture tile5; 
    private Texture tile1 = null; 
    private Texture tile2 = null; 
    private Texture tile3 = null; 
    private int textureIndex=0; 
    private boolean flipX; 
    private boolean moving; 
    private boolean onTheFloor; 
    private float verticalSpeed = 0; 
    private Level level; 
    protected Texture [] tile; 

    public Player(Level level) { 
     this.level = level; 
     tile1 = new Texture("idle.PNG"); 
     tile2 = new Texture("walk a.PNG"); 
     tile3 = new Texture("walk b.PNG"); 
     tile4= new Texture("walk c.PNG"); 
     tile5= new Texture("walk d.PNG"); 
     tile = new Texture[]{tile1, tile2, tile3,tile4,tile5}; 
     character.x = level.getpInitialX(); 
     character.y = level.getpInitialY(); 
     character.width = 80; 
     character.height = 80; 
    } 

    public void act(float delta) { 
     if (!onTheFloor) { 
      verticalSpeed += delta * Constants.GRAVITY; 
      character.y = character.y + verticalSpeed * delta; 
     } 

     checkCollisions(level.getPlatforms()); 

     if (moving) { 
      if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) 
      { 
       character.x -= Constants.INC_X; 
      } 

      if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { 
       character.x += Constants.INC_X; 
      } 
     } 

     if (character.y < 0) { 
      character.y = 0; 
      verticalSpeed = 0; 
      onTheFloor = true; 
     } 
    } 

    public void draw(SpriteBatch batch) 
    { 
     batch.draw(tile[textureIndex], 
       character.x, character.y, 
       character.width/2, character.height/2, 
       character.width, character.height, 
       1, 1, 
       0, 
       0, 0, 
       (int) character.width, (int) character.height, 
       flipX , false 
     ); 

     if (Constants.DEBUG) 
     { 
      drawDebug(batch); 
     } 
    } 

    private void checkCollisions(List<Platform> platforms) 
    { 
     if (verticalSpeed > 0) { 
      for (Platform platform : platforms) 
      { 
       if (platform.getBoundingBox().contains(character.x, character.y + character.height) || 
         platform.getBoundingBox().contains(character.x + character.width, character.y + character.height)) 
       { 
        character.y = platform.getBoundingBox().y - character.height; 
        verticalSpeed = 0; 
       } 
      } 
     } else 
     { 
      onTheFloor = false; 
      for (Platform platform : platforms) 
      { 
       if (platform.getBoundingBox().contains(character.x, character.y) || 
         platform.getBoundingBox().contains(character.x + character.width, character.y)) 
       { 
        character.y = platform.getBoundingBox().y + platform.getBoundingBox().height; 
        verticalSpeed = 0; 
        onTheFloor = true; 
       } 
      } 
     } 

    } 

    @Override 
    public boolean keyDown(int keycode) 
    { 
     if (keycode == Input.Keys.LEFT) 
     { 
      character.x -= Constants.INC_X; 
      flipX = true; 
      moving = true; 
      textureIndex++; 

     } 
     if (keycode == Input.Keys.RIGHT) 
     { 
      character.x += Constants.INC_X; 
      flipX = false; 
      moving = true; 
      textureIndex++; 
     } 
     if (keycode == Input.Keys.SPACE) 
     { 
      verticalSpeed = Constants.JUMP_SPEED; 
      onTheFloor = false; 
     } 
     return false; 
    } 

    @Override 
    public boolean keyUp(int keycode) 
    { 
     return false; 
    } 

    @Override 
    public boolean keyTyped(char character) 
    { 
     return false; 
    } 

    @Override 
    public boolean touchDown(int screenX, int screenY, int pointer, int button) 
    { 
     return false; 
    } 

    @Override 
    public boolean touchUp(int screenX, int screenY, int pointer, int button) 
    { 
     return false; 
    } 

    @Override 
    public boolean touchDragged(int screenX, int screenY, int pointer) 
    { 
     return false; 
    } 

    @Override 
    public boolean mouseMoved(int screenX, int screenY) 
    { 
     return false; 
    } 

    @Override 
    public boolean scrolled(int amount) 
    { 
     return false; 
    } 
} 

回答

0

我相信this教程做了解釋的一個相當不錯的工作libGDX的Animation類是如何工作的,這是我會強烈建議您使用。