gpt4 book ai didi

java - 无法弄清楚如何使用 isAnimationFinished() 方法(LibGDX)

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:53 27 4
gpt4 key购买 nike

我在让我的 isAnimationFinished() 在我正在处理的程序中完成工作时遇到一些问题。目前,似乎总是采用这种方法。动画的另一个问题是它几乎立即遍历所有 Sprite ,我希望将每一帧延长约 0.25 秒左右。我的相关代码如下,任何其他问题或帮助将不胜感激!

渲染调用:

public void render(float delta) 
runTime += delta;
world.update(delta);
renderer.render(delta, runTime);

动画创作:

TextureRegion[] waterGraphics = { water1, water2, water3 };
waterAnimation = new Animation(0.25f, waterGraphics);
waterAnimation.setPlayMode(Animation.PlayMode.NORMAL);

动画绘制:

if(!waterAnimation.isAnimationFinished(runTime)) {
//System.out.println("1");
batcher.draw(waterAnimation.getKeyFrame(runTime), penguin.getX()+30,
penguin.getY(), penguin.getWidth() / 2.0f,
penguin.getHeight() / 2.0f, penguin.getWidth()*5, penguin.getHeight()*5,
1, 1, penguin.getRotation()); // Don't leave at *5.....
simpleCounter++;
}

谢谢

最佳答案

您可以使用我在项目中使用的这个动画类。要检查是否完成,您可以通过 getCurrentFrame() 方法检查最后一帧是否正在运行。

import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Animation {

private Sprite[] frames;
private float time;
private float delay;
private int currentframe;
private int timesplayed;

public Animation(){}

public Animation(TextureRegion[] frames){
this(frames, 1/12);
}

public Animation(TextureRegion[] frames,float delay){
this.setFrames(frames, delay);
}

public Animation(Sprite []a,float delay){

this.frames=a;
this.delay=delay;
time=0;
currentframe=0;
timesplayed=0;
}

public void setFrames(TextureRegion[] frames,float delay){
this.frames=new Sprite[frames.length];
for(int i=0;i<frames.length;i++){
this.frames[i]=new Sprite(frames[i]);
this.frames[i].getTexture().setFilter(TextureFilter.Linear,TextureFilter.Linear);
}

this.delay=delay;
time=0;
currentframe=0;
timesplayed=0;

}

public void update(float dt){
if(delay<=0)return;
time+=dt;
while(time >= delay)step();

}

private void step() {
time-=delay;
currentframe++;
if(currentframe==frames.length)
currentframe=0;
timesplayed++;
}

public Sprite getFrames() {
return frames[currentframe];
}



public float getTime() {
return time;
}

public void setTime(float time) {
this.time = time;
}
public float getDelay() {
return delay;
}


public void setDelay(float delay) {
this.delay = delay;
}

public int getCurrentframe() {
return currentframe;
}

public void setCurrentframe(int currentframe) {
this.currentframe = currentframe;
}

public int getTimesplayed() {
return timesplayed;
}

public void setTimesplayed(int timesplayed) {
this.timesplayed = timesplayed;
}


}

关于java - 无法弄清楚如何使用 isAnimationFinished() 方法(LibGDX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41401616/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com