Android 安卓:使用libgdx/universaltween引擎发布后移动到触摸屏位置
我正在学习将libgdx与universal tween engine一起使用,但我还无法想出如何触摸(或单击桌面应用程序)屏幕上的一个点,并让纹理一直移动到触摸的位置,而不保持触摸或单击活动,直到到达终点 当触摸事件启动时,动画开始,图形向该位置移动。如果开始触摸和拖动,图形将跟随手指/鼠标指针。如果我触摸一个点,图形将向该点移动,直到触摸被释放。然后,当触摸被释放时,它停止在原来的位置 我希望触摸和释放,并将图形移动到触摸点,但可能不了解关于tween引擎实现的一些内容。我已经在下面粘贴了tweening代码Android 安卓:使用libgdx/universaltween引擎发布后移动到触摸屏位置,android,libgdx,tween,Android,Libgdx,Tween,我正在学习将libgdx与universal tween engine一起使用,但我还无法想出如何触摸(或单击桌面应用程序)屏幕上的一个点,并让纹理一直移动到触摸的位置,而不保持触摸或单击活动,直到到达终点 当触摸事件启动时,动画开始,图形向该位置移动。如果开始触摸和拖动,图形将跟随手指/鼠标指针。如果我触摸一个点,图形将向该点移动,直到触摸被释放。然后,当触摸被释放时,它停止在原来的位置 我希望触摸和释放,并将图形移动到触摸点,但可能不了解关于tween引擎实现的一些内容。我已经在下面粘贴了t
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;
}
}
}
最初,我在条件for中有tweening代码,没有使用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);
}
我也尝试过使用,但图形只会稍微向接触点移动
我已经为此挣扎了几个小时,如果有人能给我指出正确的方向,我将不胜感激
谢谢。我试图以错误的方式实现此行为。我需要使用来自的
touched()
而不是istoched
或justtouch()
我在主libgdx项目中的主类(实现ApplicationListener的类)中创建了一个类,该类实现了GestureDetector(称之为touchListener()
),并将x和y捕获代码放入toucDown
中(我注意到tap()
也被触发)。我将tween函数(实际tween、调用registerAccessor()
,以及创建新的tween管理器)移动到touchListener()的update()
方法中
我在主libgdx类的render()
循环中添加了对touchlister()
更新函数的调用
我怀疑我这样做是不是最好的方式,但我希望这对将来的其他人有帮助
Tween.registerAccessor(Plane.class, new TextureAccessor());
TweenManager planeManager = new TweenManager();
这两行应该放在create()
方法中,而不是render()
一行!在这里,您在每个框架上实例化一个新的管理器,您只需要一个管理器,仅此而已,而不是一大群管理器
此外,您需要在每一帧上更新管理器,而不仅仅是当animateOn
为真时,否则您需要按住手指
正确的代码如下所示,从中学习,您将更好地了解Tween引擎的工作原理:)
这不是最好的方法,事实上,这很有效,因为你很幸运:)你真的不应该在触控式听众中创建一个新的tween经理。一个项目只需要一名经理。它就像一个雪碧手表,或者一个照相机。您只创建了它们的一个实例:)请查看我的答案以获得正确的解决方案,它将指导您使用tween引擎。感谢您的回复和正确答案。在我的手机上进行测试时,我会遇到内存问题,并摆脱了遍布各地的多个tween经理。这是一个基于回合的模块的开始,我可能应该把它排除在外。谢谢你的帮助和伟大的图书馆。
// 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);
}
}