2013-04-17 3 views

Antwort

9

Genau wie ich Sie nicht animierte Schauspieler gefunden, so habe ich mich:

AnimatedActor.java:

public class AnimatedActor extends Image 
{ 
    private final AnimationDrawable drawable; 

    public AnimatedActor(AnimationDrawable drawable) 
    { 
     super(drawable); 
     this.drawable = drawable; 
    } 

    @Override 
    public void act(float delta) 
    { 
     drawable.act(delta); 
     super.act(delta); 
    } 
} 

AnimationDrawable.java:

class AnimationDrawable extends BaseDrawable 
{ 
    public final Animation anim;  
    private float stateTime = 0; 

    public AnimationDrawable(Animation anim) 
    { 
     this.anim = anim; 
     setMinWidth(anim.getKeyFrameAt(0).getRegionWidth()); 
     setMinHeight(anim.getKeyFrameAt(0).getRegionHeight()); 
    } 

    public void act(float delta) 
    { 
     stateTime += delta; 
    } 

    public void reset() 
    { 
     stateTime = 0; 
    } 

    @Override 
    public void draw(SpriteBatch batch, float x, float y, float width, float height) 
    { 
     batch.draw(anim.getKeyFrame(stateTime), x, y, width, height); 
    } 
} 
+0

Ich habe etwas ähnliches. Ich werde warten und sehen, ob jemand auf eine tatsächliche Schauspieler Klasse hinweisen kann, aber wenn nicht, werde ich dies als die Antwort markieren. – Lokiare

+0

Vielen Dank für Ihr Schnipsel, lieber Herr. –

+0

Dies scheint ein wenig langsamer als "direkte" Ansatz: TextureRegion frame2 = bird_07.getKeyFrame (stateTime, true); stage.getSpriteBatch(). Begin(); stage.getSpriteBatch(). Draw (currentFrame, 1000, 700); irgendeine Idee warum? – atok

17

ich einfach ein erstellt " AnimatedImage "actor-Klasse, die nur eine Animation als Argument akzeptiert (keine Notwendigkeit für eine benutzerdefinierte Drawable-Klasse). Ich denke, diese Lösung ist viel einfacher als die obige.

AnimatedImage.java:

public class AnimatedImage extends Image 
{ 
    protected Animation animation = null; 
    private float stateTime = 0; 

    public AnimatedImage(Animation animation) { 
     super(animation.getKeyFrame(0)); 
     this.animation = animation; 
    } 

    @Override 
    public void act(float delta) 
    { 
     ((TextureRegionDrawable)getDrawable()).setRegion(animation.getKeyFrame(stateTime+=delta, true)); 
     super.act(delta); 
    } 
} 
Verwandte Themen