我正在学习将 libgdx 与通用补间引擎一起使用,但一直无法弄清楚如何触摸(或单击桌面应用程序)屏幕上的一个点并让纹理一直移动到触摸的位置,而不保持触摸或点击 Activity ,直到到达终点。
当触摸事件发起时,动画开始,图形向该位置移动。如果启动触摸和拖动,图形将跟随手指/鼠标指针。如果我触摸一个点,图形将向该点移动,直到触摸被释放。然后它会停在释放触摸时的位置。
我正在寻找触摸和释放并将图形移动到触摸点,并且我可能不了解补间引擎实现的某些内容。我在下面粘贴了补间代码。
public void render() {
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(texture.getTexture(), texture.getBoundingBox().x, texture.getBoundingBox().y);
batch.end();
Tween.registerAccessor(Plane.class, new TextureAccessor());
TweenManager planeManager = new TweenManager();
float newX = 0;
float newY = 0;
boolean animateOn = false;
if(Gdx.input.isTouched()) {
newX = Gdx.input.getX();
newY = Gdx.input.getY();
animateOn = true;
}
if (animateOn == true && (texture.getX() != newX || texture.getY() != newY)) {
Tween.to(texture, TextureAccessor.POSITION_XY, 10)
.target(newX, newY)
.ease(TweenEquations.easeNone)
.start(planeManager);
planeManager.update(1);
if (texture.getX() == newX && texture.getY() == newY) {
animateOn = false;
}
}
}
最初,我在 isTouched()
的条件语句中有补间代码并且没有使用 newX
、newY
或 animateOn
变量。我认为使用 isTouched()
仅设置新坐标和动画状态将使循环触发补间。旧代码如下所示:
if(Gdx.input.isTouched()) {
newX = Gdx.input.getX();
newY = Gdx.input.getY();
Tween.to(texture, TextureAccessor.POSITION_XY, 10)
.target(newX, newY)
.ease(TweenEquations.easeNone)
.start(planeManager);
planeManager.update(1);
}
我也试过使用 justTouched()
, 但图形只会非常轻微地向触摸点移动。
我已经为此苦苦挣扎了几个小时,如果有人能指出正确的方向,我将不胜感激。
谢谢。
最佳答案
Tween.registerAccessor(Plane.class, new TextureAccessor());
TweenManager planeManager = new TweenManager();
这两行应该放在 create()
方法中,而不是 render()
方法中!在这里,您要在每一帧上实例化一个新的管理器,您只需要一个管理器,仅此而已,不需要一大群管理器!
此外,您需要在每一帧更新管理器,而不仅仅是当 animateOn
为真时,否则您需要按住手指...
正确的代码如下,学习它,你会更好地理解Tween Engine是如何工作的:)
// Only one manager is needed, like a Spritebatch
private TweenManager planeManager;
public void create() {
Tween.registerAccessor(Plane.class, new TextureAccessor());
planeManager = new TweenManager();
}
public void render() {
// The manager needs to be updated on every frame.
planeManager.update(Gdx.graphics.getDeltaTime());
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(texture.getTexture(), texture.getBoundingBox().x, texture.getBoundingBox().y);
batch.end();
// When the user touches the screen, we start an animation.
// The animation is managed by the TweenManager, so there is
// no need to use an "animateOn" boolean.
if (Gdx.input.justTouched()) {
// Bonus: if there is already an animation running,
// we kill it to prevent conflicts with the new animation.
planeManager.killTarget(texture);
// Fire the animation! :D
Tween.to(texture, TextureAccessor.POSITION_XY, 10)
.target(Gdx.input.getX(), Gdx.input.getY())
.ease(TweenEquations.easeNone)
.start(planeManager);
}
}
关于android:使用 libgdx/universal-tween-engine 发布后移动到触摸位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11179931/