Java 工厂生成的零关键帧动画,用于直接实例化
类定义并包含libgdx动画:Java 工厂生成的零关键帧动画,用于直接实例化,java,animation,libgdx,Java,Animation,Libgdx,类定义并包含libgdx动画: public class ScreenObject { protected int width; protected int height; protected float stateTime; protected String spriteSheetFile; protected int spriteSheetCOLS; protected int spriteSheetROWS; protected fl
public class ScreenObject {
protected int width;
protected int height;
protected float stateTime;
protected String spriteSheetFile;
protected int spriteSheetCOLS;
protected int spriteSheetROWS;
protected float frameDuration;
protected Texture spriteSheet;
protected TextureRegion[] frames;
protected Animation animation;
public void makeAnimation() {
spriteSheet = new Texture(spriteSheetFile);
TextureRegion[][] temp = TextureRegion.split(spriteSheet, width, height);
frames = new TextureRegion[spriteSheetROWS * spriteSheetCOLS];
int index = 0;
for ( int i = 0; i < spriteSheetROWS; i++ ) {
for ( int j = 0; j < spriteSheetCOLS; j++ ) {
frames[index++] = temp[i][j];
}
}
animation = new Animation(frameDuration, frames);
stateTime = 0;
}
public void draw(SpriteBatch batch) {
TextureRegion currentFrame = animation.getKeyFrame(stateTime, true);
batch.draw(currentFrame, 0, 0);
}
}
我在draw()调用中得到一个零除错误:
动画的关键帧数组中没有帧!但这让我小小的心灵难以置信。我已经验证了工厂生成并存储在ScreenObject[]数组中的ScreenObject是否由setter设置了适当的值,然后我调用了使用这些值制作动画的相同makeAnimation()方法。。。但它只能以一种方式工作,而不能以另一种方式工作
我曾尝试在getKeyFrame()上搜索其他带有算术异常的问题,以及创建ScreenObject类的两种不同方法可能产生的影响,但我在这里真的不知所措。我最好的猜测是,在这一过程中,
spriteSheetROWS
或spriteSheetCOLS
设置为零,或从未更改其默认值zero.Yep。两个setter都需要设置columns变量。当我测试setter方法时,我使用了C&P,在反思中,我想我可能不应该这样做。曾经非常感谢你。
sof = new ScreenObjectFactory();
planets = new Planet[] { sof.randomPlanet() };
planets[0].makeAnimation();
Exception in thread "LWJGL Application" java.lang.ArithmeticException: / by zero
at com.badlogic.gdx.graphics.g2d.Animation.getKeyFrameIndex(Animation.java:142)