2014-07-13 5 views
0

En essayant d'avoir une image-objet 2-d, descendante qui tourne et est animée, j'ai la classe principale où elle est rendue, et une classe 'assets' distincte où elle est créée. La rotation fonctionne, mais l'image-objet reste sur le même cadre. J'ai utilisé le wiki libgdx pour essayer d'animer mon sprite https://github.com/libgdx/libgdx/wiki/2D-AnimationAnimation de sprites Libgdx Java

Voici le code dans la classe d'actifs:

public class Asset implements ApplicationListener, Screen{ 

    public static Texture walkSheet; 




    private static final int FRAME_COLS = 4;  
    private static final int FRAME_ROWS = 2;  

    static Animation   walkAnimation;  
    static TextureRegion[]   walkFrames;  
    static TextureRegion   currentFrame;  
    static SpriteBatch spriteBatch; 

    static float stateTime;     


    public static void load(){ 
     walkSheet = new Texture(Gdx.files.internal(".png")); 


     TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth()/FRAME_COLS, walkSheet.getHeight()/FRAME_ROWS);    // #10 
     walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS]; 
     int index = 0; 
     for (int i = 0; i < FRAME_ROWS; i++) { 
      for (int j = 0; j < FRAME_COLS; j++) { 
       walkFrames[index++] = tmp[i][j]; 
      } 
     } 
     walkAnimation = new Animation(0.1f, walkFrames);  // #11 
     spriteBatch = new SpriteBatch();    // #12 
     stateTime = 0f; 
     stateTime += Gdx.graphics.getDeltaTime(); 
     currentFrame = walkAnimation.getKeyFrame(stateTime, true); 
    } 

Et voici comment il est rendu:

game.batch.draw(Asset.currentFrame, x, y, (float)85, (float)85, (float)170, (float)170, (float)1, (float)1, (float)angleDegrees + 270); 

Que dois-je besoin de faire pour animer correctement l'image-objet?

Répondre

1

Le cadre ne s'anime jamais car vous ne le mettez pas à jour. Dans la méthode load() vous avez fait ce qui suit:

stateTime += Gdx.graphics.getDeltaTime(); 
currentFrame = walkAnimation.getKeyFrame(stateTime, true); 

mais cela s'exécuté qu'une seule fois lorsque vous appelez la méthode load().

En tant que votre classe Asset implémente l'interface Screen vous devez avoir mis en œuvre la méthode abstraite render(float delta), de sorte que vous devez mettre à jour le cadre dans cette méthode comme suit:

public void render(float delta) 
{ 
    stateTime += delta; 
    currentFrame = walkAnimation.getKeyFrame(stateTime, true); 
    // Then render the frame as follows 
    batch.draw(currentFrame, x, y, (float)85, (float)85, (float)170, (float)170, (float)1, (float)1, (float)angleDegrees + 270); 
} 
0
// pass the array of movements to AnimatedActor 
// animationFrames = walkSheetArray[moveDirection]; 
// animation = new Animation(1f/5f, animationFrames); 
// myAnimatedActor = new AnimatedActor(animation, rotation) 

public class AnimatedActor extends Image { 
private float stateTime = 0; 
Animation animation; 
public AnimatedActor(Animation animation, float rotation) { 
super(animation.getKeyFrame(0)); 
this.animation = animation; 
this.rotation = rotation; 
} 
@Override 
public void act(float delta) { 
((TextureRegionDrawable) getDrawable()).setRegion(animation.getKeyFrame(stateTime += delta, true)); 
myAnimatedActor.setRotation(rotation); 
super.act(delta); 
    } 
} 

walk frames 8x8

+0

l'image de http://www.reinerstilesets.de/ –