我正在学习使用libgdx和universal-tween-engine,但是一直没有弄清楚如何触摸(或在桌面应用上单击)屏幕上的一个点,并使纹理移动到触摸位置,而不保持触摸或点击处于活动状态,直到到达终点。
当触摸事件被启动时,动画开始,图形向位置移动。如果启动触摸并拖动,则图形将跟随手指/鼠标指针移动。如果我接触一个点,图形将朝向该点移动,直到释放触摸。然后它停在触摸释放时的位置。
我想要触摸并释放,并使该图像移动到触摸点,可能是因为我没有完全理解缓动引擎的实现。我已经粘贴了缓动代码如下。
我还尝试使用
当触摸事件被启动时,动画开始,图形向位置移动。如果启动触摸并拖动,则图形将跟随手指/鼠标指针移动。如果我接触一个点,图形将朝向该点移动,直到释放触摸。然后它停在触摸释放时的位置。
我想要触摸并释放,并使该图像移动到触摸点,可能是因为我没有完全理解缓动引擎的实现。我已经粘贴了缓动代码如下。
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()
,但是图形只会稍微朝被触摸的点移动一点点。
我已经为此苦苦挣扎了几个小时,如果有人能指点我一下就太感激了。
谢谢。