Android:libgdx / universal-tween-engine 释放后移动到触摸位置

4
我正在学习使用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()的条件语句中,并没有使用newXnewYanimateOn变量。我认为只使用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(),但是图形只会稍微朝被触摸的点移动一点点。

我已经为此苦苦挣扎了几个小时,如果有人能指点我一下就太感激了。

谢谢。

2个回答

6
Tween.registerAccessor(Plane.class, new TextureAccessor());
TweenManager planeManager = new TweenManager();

这两行代码应该放在create()方法中,而不是render()中!在此处,您每一帧都会实例化一个新的管理器,但您只需要一个管理器即可,不需要成千上万个!
另外,您需要在每一帧更新管理器,而不仅仅是在animateOn为true时更新,否则您需要一直按住鼠标键...
正确的代码如下,学习它,您将更好地了解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);
    }
}

感谢您的回复和正确的答案。在我的手机上测试时,我遇到了内存问题,并且消除了所有地方的多个tween管理器。animateOn是用于回合制模块的开头,我可能应该将其从问题中省略。感谢您的帮助和出色的库。 - nils

2
我试图以错误的方式实现这个行为。我需要使用GestureListener中的touchDown(),而不是使用isTouchedjustTouched()

我在主要的libgdx项目中创建了一个实现GestureDetector的类(称其为touchListener()),并将x和y捕获代码放在toucDown中(我注意到tap()也被触发)。我将缓动函数(实际的缓动、对registerAccessor()的调用和新缓动管理器的创建)移动到touchListener()update()方法中。

我在主要的libgdx类的render()循环中添加了对touchListener()的更新函数的调用。

我怀疑我没有以最好的方式完成这个任务,但我希望它对未来的某些人有所帮助。


这并不是最好的方法,实际上它能够工作只是因为你很幸运 :) 你真的不应该在触摸监听器中创建一个新的缓动管理器。在项目中只需要一个管理器。就像Spritebatch或Camera一样。你只需要创建一个实例即可 :) 查看我的答案以获取正确的解决方案,它将指导你使用缓动引擎。 - Aurelien Ribon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接