android:使用libgdx / universal-tween-engine在发布后移动到触摸位置

时间:2012-06-24 18:06:32

标签: android libgdx tween

我正在学习使用带有通用补间引擎的libgdx,并且无法弄清楚如何触摸(或点击桌面应用程序)屏幕上的一个点并让纹理一直移动到触摸的位置不保持触摸或单击活动直到到达终点。

启动触摸事件后,动画开始,图形向该位置移动。如果启动了触摸和拖动,图形将遵循手指/鼠标指针。如果我触摸一个点,图形将朝向该点移动,直到触摸被释放。然后它会在触摸释放时停止。

我正在寻找触摸和释放并将图形移动到触摸点,我可能不了解补间引擎实现的某些内容。我已粘贴下面的补间代码。

    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 个答案:

答案 0 :(得分:6)

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

这两行应该使用create()方法,而不是render()方法!在这里,你要在每一帧上实例化一个新的经理,你只需要一个经理,这就是全部,而不是他们的军队!

此外,您需要在每一帧更新经理,而不仅仅是animateOn为真,否则您需要按住手指......

正确的代码如下,从中学习,您将更好地理解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);
    }
}

答案 1 :(得分:2)

我试图以错误的方式实现此行为。我需要使用GestureListener中的isTouched,而不是使用justTouched()touchDown()

我在主libgdx项目中创建了一个在我的主类(实现ApplicationLisetener的实现)中实现GestureDetector(称之为touchListener())的类,并将x和y捕获代码放在{{1}中(我注意到toucDown也被触发了)。我将补间函数(实际补间,对tap()的调用以及新补间管理器的创建)移动到registerAccessor()的{​​{1}}方法中。

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

我怀疑我这样做是最好的方法,但我希望将来对其他人有所帮助。