2013-04-17 17 views

Odpowiedz

9

tak jak ty Nie mogę znaleźć animowany aktor więc stworzyłem sobie:

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

zrobiłem coś podobnego. Zaczekam i zobaczę, czy ktoś może wskazać rzeczywistą klasę Aktorów, ale jeśli nie, zaznaczę to jako odpowiedź. – Lokiare

+0

Dziękuję bardzo za twój fragment, uprzejmy panie. –

+0

To wydaje się nieco wolniejsze niż podejście "bezpośrednie": TextureRegion frame2 = bird_07.getKeyFrame (stateTime, true); stage.getSpriteBatch(). Begin(); stage.getSpriteBatch(). Draw (currentFrame, 1000, 700); Jakiś pomysł, dlaczego? – atok

17

ja po prostu stworzył " AnimatedImage "klasa aktorska, która przyjmuje tylko animację jako argument (bez potrzeby stosowania niestandardowej klasy Drawable). Myślę, że to rozwiązanie jest o wiele prostsze niż powyższe.

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); 
    } 
} 
Powiązane problemy