我正在使用 Libgdx 游戏库开发游戏。 我在尝试使用从一个角点到接触点制作动画时遇到了这个问题 libgdx。 使用本教程了解 Libgdx 动画基础知识的基础知识 Link .
我无法找到 libgdx 中的动画是如何工作的,帧的正常移动发生但如何在触摸屏幕时启动单次正常动画。 在此先感谢您的帮助。
编辑: 我的类(class)实现屏幕 这是我试过的
在类默认构造函数中
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;
}
}
调用touch方法后做动画
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());
}
}
最佳答案
您可以使用 InputListener用于触摸事件并使用它来控制您的动画。
下面是简单的方法。当然,更合适的做法是扩展 Animation 类并将所有动画逻辑移到那里。
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);
您只需从动画中获取适当的关键帧并批量绘制即可。
关于java - 如何在 libgdx 中为从 1 个角到触摸点的 Sprite 表/纹理区域设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24775329/