如何使用libgdx完成动画
我正在尝试用libGDX实现一个简单的动画,而我目前只专注于一件事。假设我有一堆雪碧,需要一些时间才能完成。例如,大约有30个类似此链接的精灵: 但是,在动画完成之前,会按一些关键点。对于平滑动画,我希望在开始下一组动画之前完成30帧,以防止突然停止 所以我的问题是如何在libGDX中实现这一点?我目前的想法是扩展如何使用libgdx完成动画,libgdx,Libgdx,我正在尝试用libGDX实现一个简单的动画,而我目前只专注于一件事。假设我有一堆雪碧,需要一些时间才能完成。例如,大约有30个类似此链接的精灵: 但是,在动画完成之前,会按一些关键点。对于平滑动画,我希望在开始下一组动画之前完成30帧,以防止突然停止 所以我的问题是如何在libGDX中实现这一点?我目前的想法是扩展Animation类,它将跟踪我有多少帧以及渲染了多少帧,然后显示其余的帧。或者使用isAnimationFinished(float stateTime)函数(尽管我在使用该函数时运
Animation
类,它将跟踪我有多少帧以及渲染了多少帧,然后显示其余的帧。或者使用isAnimationFinished(float stateTime)
函数(尽管我在使用该函数时运气不好)
我所看到的像superjumper这样的例子只有很少的动画,而且变化不大
另外,是否有方法保存TextureAtlas.createSprites
方法中的精灵列表并将其与动画类一起使用?如果没有,提供此功能的目的是什么
谢谢您可以使用
animation.isAnimationFinished(stateTime);
查看动画是否完成
对于精灵:personnaly我使用TextureAtlas中的TextureRegion,并将它们存储在一个数组中用于动画我创建了一个类
animateImage
,该类扩展了图像
以自动在图像中绘制精灵。我的代码如下所示:
public class AnimatedImage extends Image{
private Array<Array<Sprite>> spriteCollection;
private TextureRegionDrawable drawableSprite;
private Animation _animation;
private boolean isLooping;
private float stateTime;
private float currentTime;
public AnimatedImage(Array<Array<Sprite>> _sprites, float animTime, boolean _looping){
// set the first sprite as the initial drawable
super(_sprites.first().first());
spriteCollection = _sprites;
// set first collection of sprite to be the animation
stateTime = animTime;
currentTime = 0;
_animation = new Animation(stateTime, spriteCollection.first());
// set if the anmation needs looping
isLooping = _looping;
drawableSprite = new TextureRegionDrawable(_animation.getKeyFrame(currentTime));
this.setDrawable(drawableSprite);
}
public void update(float delta){
currentTime += delta;
TextureRegion currentSprite = _animation.getKeyFrame(currentTime, isLooping);
drawableSprite.setRegion(currentSprite);
}
public void changeToSequence(int seq){
// reset current animation time
resetTime();
_animation = new Animation(stateTime, spriteCollection.get(seq));
}
public void changeToSequence(float newseqTime, int seq){
_animation = new Animation(newseqTime, spriteCollection.get(seq));
}
public void setRepeated(boolean _repeat){
isLooping = _repeat;
}
public boolean isAnimationFinished(){
return _animation.isAnimationFinished(currentTime);
}
public void resetTime(){
currentTime = 0;
}
}
_img.addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){
_img.changeToSequence(1);
return true;
}
});
然后我添加InputListener,如下所示:
public class AnimatedImage extends Image{
private Array<Array<Sprite>> spriteCollection;
private TextureRegionDrawable drawableSprite;
private Animation _animation;
private boolean isLooping;
private float stateTime;
private float currentTime;
public AnimatedImage(Array<Array<Sprite>> _sprites, float animTime, boolean _looping){
// set the first sprite as the initial drawable
super(_sprites.first().first());
spriteCollection = _sprites;
// set first collection of sprite to be the animation
stateTime = animTime;
currentTime = 0;
_animation = new Animation(stateTime, spriteCollection.first());
// set if the anmation needs looping
isLooping = _looping;
drawableSprite = new TextureRegionDrawable(_animation.getKeyFrame(currentTime));
this.setDrawable(drawableSprite);
}
public void update(float delta){
currentTime += delta;
TextureRegion currentSprite = _animation.getKeyFrame(currentTime, isLooping);
drawableSprite.setRegion(currentSprite);
}
public void changeToSequence(int seq){
// reset current animation time
resetTime();
_animation = new Animation(stateTime, spriteCollection.get(seq));
}
public void changeToSequence(float newseqTime, int seq){
_animation = new Animation(newseqTime, spriteCollection.get(seq));
}
public void setRepeated(boolean _repeat){
isLooping = _repeat;
}
public boolean isAnimationFinished(){
return _animation.isAnimationFinished(currentTime);
}
public void resetTime(){
currentTime = 0;
}
}
_img.addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){
_img.changeToSequence(1);
return true;
}
});
希望有帮助。使用tween引擎制作此类动画。它有很好的文档记录,并且libgdx支持它。。用谷歌搜索一下,你可以找到很多使用libgdx的例子。。希望它能帮助你 您能否提供有关如何使用动画的更多上下文?isAnimationFinished(float stateTime)非常简单,可能是您使用了错误的东西。如何检测动画何时“正常”完成?