2018编辑:在较新版本的LibGDX中,Animation类不仅限于TextureRegions.它可以为任何通用的对象数组设置动画,因此您可以创建Animation< Sprite> ;,尽管您需要确保每次获取关键帧Sprite时都应用适当的比例.就个人而言,我认为应该避免Sprite类,因为它会使游戏状态的资源(图像)更加丰富.
您没有说如何绘制动画,但大概是您使用的是一种不了解子图形比例的方法.
由于Animation类存储了TextureRegions,因此如果您从动画中获得一帧进行绘制,如下所示:
spriteBatch.draw(animation.getKeyFrame(time), x, y);
则该sprite批处理不知道它是Sprite的实例,仅知道它是TextureRegion的实例,其中不包含缩放信息.
一种快速而肮脏的方法是这样的:
Sprite sprite = (Sprite)animation.getKeyFrame(time));
spriteBatch.draw(sprite, x, y, 0, 0, sprite.width, sprite.height, sprite.scaleX, sprite.scaleY, 0);
但是,如果您想要一种行为类似于精灵的动画,则可以对其进行子类化,以使其存储缩放数据(如果需要,还可以旋转和定位)并绘制自身.然后,您完全不必担心Sprite实例.
例如:
public class SpriteAnimation extends Animation {
float scaleX = 1;
float scaleY = 1;
/*...duplicate and call through to super constructors here...*/
public void setScaling(float scale){
scaleX = scale;
scaleY = scale;
}
public void draw (float stateTime, Batch batch, float x, float y) {
TextureRegion region = getKeyFrame(stateTime);
batch.draw(region, x, y, region.width*scaleX, region.height*scaleY);
}
}
您可以根据自己的喜好自定义此属性,以存储x和y,旋转,原点等,如果需要,可以像精灵一样.创建它时,根本不会使用Sprite类.你会做这样的事情.
frame1 = new TextureRegion(texture, 764, 75, 141, -74);
frame2 = new TextureRegion(texture, 907, 75, 133, -75);
TextureRegion[] frames = { frame1, frame2 };
mySpriteAnimation = new SpriteAnimation(0.06f, frames);
mySpriteAnimation.setScaling(scaler);
mySpriteAnimation.setPlayMode(Animation.PlayMode.LOOP);