java - Add animation into Sprite.draw(SpriteBatch) method -


if possible add animation sprite.draw(spritebatch) method. according proper scaling in multidimensial screens use viewports , displaying textures this:

player= new sprite(new texture("player.jpg"));         player.setsize(player_size,player_size);         player.setposition(0,0);    @override     public void render(spritebatch sb) {         sb.setprojectionmatrix(camera.combined);         sb.begin();         player.draw(sb);         sb.end();     } 

i want animate movement of texture whenever move. after searching found 1 way required spritebatch.draw(animation.getkeyframe(elapsed_time),position.x,position.y), there way this?

the simple answer animation class provided libgdx https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/animation.html

here goes how works:

  • create sprite map animation frames using favorite graphic tool.
  • knowing dimensions split sprite map, i.e.:

    texture spritemap = newtexture(gdx.files.internal("images/sprite-map.png")); textureregion[][] spriteposition = textureregion.split(spritemap, frame_width, frame_height); 
  • next, create animation class animation, example bomb exploding, need pick right coordinates sprite map, sprite map 2 dimensional matrix. can example:

    textureregion[] bombsprites = new textureregion[4]; bombsprites[0] = spriteposition[5][6]; bombsprites[1] = spriteposition[5][7]; bombsprites[2] = spriteposition[5][8]; bombsprites[3] = spriteposition[5][9]; animation bombanimation = new animation(0.1f, bombsprites); 
  • having animation prepared, need have class created extends actor class. i.e.:

    public class bomb extends actor {   ... (some fields , methods)  @override  public void draw(batch batch, float parentalpha) {   ...  } 
  • you need override act method:

    @override public void act(float delta) {    this.statetime += delta; } 
  • after use batch.draw method in overridden draw() method invoke

    batch.draw(animation.getkeyframe(this.statetime, true) 

that's what's requirement simple animation

if need compete example please read tutorial created william mora http://williammora.com/a-running-game-with-libgdx-part-1

if unclear please write in comment.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -