Java 如何在libgdx中为从1个角到接触点的精灵片/纹理区域设置动画?
我正在使用Libgdx游戏库开发一个游戏。 我在尝试使用动画从一个角点到一个接触点时遇到了这个问题 libgdx。 使用本教程了解Libgdx动画基础知识 我无法找到libgdx中的动画是如何工作的,帧的正常移动是如何发生的,但如何在触摸屏时启动单次正常动画。 提前谢谢你的帮助 编辑: 我的类实现了屏幕 这就是我试过的 类内默认构造函数Java 如何在libgdx中为从1个角到接触点的精灵片/纹理区域设置动画?,java,android,opengl-es,libgdx,android-animation,Java,Android,Opengl Es,Libgdx,Android Animation,我正在使用Libgdx游戏库开发一个游戏。 我在尝试使用动画从一个角点到一个接触点时遇到了这个问题 libgdx。 使用本教程了解Libgdx动画基础知识 我无法找到libgdx中的动画是如何工作的,帧的正常移动是如何发生的,但如何在触摸屏时启动单次正常动画。 提前谢谢你的帮助 编辑: 我的类实现了屏幕 这就是我试过的 类内默认构造函数 animation=new Animation(1/15f, atlas1.getRegions()); 在渲染方法中:- 检查触摸 public void
animation=new Animation(1/15f, atlas1.getRegions());
在渲染方法中:-
检查触摸
public void touched()
{
if(Gdx.input.isTouched())
{
touched = true;
x = Gdx.input.getX();
y = Gdx.input.getY();
velocityX = (x - animationX) / 100;
velocityY = (y - animationY) / 100;
}
}
调用动画的触摸方法后
public void anim()
{
if (touched)
{
elapsedTime += Gdx.graphics.getDeltaTime();
//animationX += velocityX;
// animationY += velocityY;
batch.draw(animation.getKeyFrame(elapsedTime, false), x, y);
animfinished=animation.isAnimationFinished(elapsedTime);
}
if(touched)
{
batch.draw(bullettouch, x, y, bullettouch.getRegionWidth(), bullettouch.getRegionHeight());
}
}
您可以使用触摸事件,并使用它来控制动画
下面是一个简单的方法。当然,更合适的做法是扩展动画类并将所有动画逻辑移到那个里
public class AnimationTest implements ApplicationListener, InputListener {
boolean touched = false;
Animation animation;
float elapsedTime = 0;
float touchedX, touchedY;
float animationX, animationY;
float velocityX, velocityY;
// ... do not forget to register InputListener here ...
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer,int button) {
touched = true;
touchedX = x;
touchedY = y;
velocityX = (touchedX - animationX) / 100;
velocityY = (touchedY - animationY) / 100;
}
// ...
@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
if (touched) {
elapsedTime += Gdx.graphics.getDeltaTime();
animationX += velocityX;
animationY += velocityY;
batch.draw(animation.getKeyFrame(elapsedTime, true), animationX, animationY);
}
batch.end();
}
}
这就是动画发生的地方:
batch.draw(animation.getKeyFrame(elapsedTime,true)、animationX、animationY)代码>
您只需从动画中获取合适的关键帧,并在批处理中绘制该关键帧。您需要指定一件事,您是否使用Scene2D?演员上的动画和你自己对象上的动画是完全不同的。我正在为不同的对象制作动画,我已经存储在pack/png中。但是你使用的是SpriteBatch还是Scene2d和actor?我使用的是SpriteBatch@Zhuinden@Zhuinden我把我尝试过的东西都放在这里了,但是如果我把循环设置为真,动画就会重复,如果我把循环设置为假,动画只会在1个位置重复。几句解释也会很有帮助。
public void anim()
{
if (touched)
{
elapsedTime += Gdx.graphics.getDeltaTime();
//animationX += velocityX;
// animationY += velocityY;
batch.draw(animation.getKeyFrame(elapsedTime, false), x, y);
animfinished=animation.isAnimationFinished(elapsedTime);
}
if(touched)
{
batch.draw(bullettouch, x, y, bullettouch.getRegionWidth(), bullettouch.getRegionHeight());
}
}