Android: moving to space after release with libgdx / universal engine

I am studying the use of libgdx with the universal tween engine and cannot figure out how to click (or click on a desktop application) a point on the screen and move the texture completely by touching, without supporting the touch or click, until the end point is reached.

When a touch event is triggered, the animation starts and the image moves to its place. The graphic will follow the finger / mouse pointer if you start tapping and dragging. If I touch the point, the graphic will move to the point until the touch key is pressed. Then he stops where he was when pressed.

I look at touch-and-release and get this graphic transition to the affected point, and probably don’t understand anything about the tween engine implementation. I have inserted the swap code below.

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; } } } 

Initially, I had tweening code inside the conditional expression for isTouched() and did not use newX , newY or animateOn . I thought using isTouched() only to set new coordinates and state of the animation, then the loop will start in the animation. The older code looked like this:

  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); } 

I also tried using justTouched() , but the graphics engine only moved slightly towards the affected point.

I struggled with this for several hours, I would really appreciate it if someone could point me in the right direction.

Thanks.

+4
source share
2 answers
 Tween.registerAccessor(Plane.class, new TextureAccessor()); TweenManager planeManager = new TweenManager(); 

These two lines should go in the create() method, and not in render() ! Here you create a new manager on each frame, you only need one manager, not an army!

In addition, you need to update the manager on each frame, and not only when animateOn true, otherwise you will need to keep your finger pressed ...

The correct code is as follows: find out how Tween Engine works :)

 // 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); } } 
+6
source

I tried to implement this behavior incorrectly. Instead of using isTouched or justTouched() I needed to use touchDown() from the GestureListener .

I created a class that implemented the GestureDetector (call it touchListener() ) inside my main class (the one that implements ApplicationLisetener) in the main libgdx project and put the capture code x and y inside toucDown (I noticed tap() also works). I moved the twin functions (actual tweening, calling registerAccessor() and creating a new twin manager) to the update() touchListener() .

I added a call to the touchListener() update function inside the render() loop of the libgdx main class.

I doubt that I did this, this is the best way, but I hope this helps someone else in the future.

+2
source

All Articles