补间以创建一个对象跟随另一个对象的效果-LibGdx

Tween to create the effect of one object follows another-LibGdx

我在屏幕上有一个对象(名为框架),它会根据我移动手指的位置向左或向右移动。

public  void handleSwipeInput() {
    if (MyInputProcessor.isTouchDown) {
        float movedir = MyInputProcessor.dist > 0 ? 1 : -1;
        float speed = 30;
        float actualSpeed = Math.abs(MyInputProcessor.dist) >= speed ? speed : Math.abs(MyInputProcessor.dist);
        if (movedir > 0) {
            frame.setX(frame.getX() + actualSpeed+2);
            MyInputProcessor.dist -= actualSpeed;

        } else {
            frame.setX(frame.getX() - actualSpeed-2);
            MyInputProcessor.dist += actualSpeed;
            }
    }

}

 @Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    dist+=screenX-start;
    start=screenX;
    isTouchDragged=true;
    return false;
}

在 update() 方法中:

 if (MyInputProcessor.isTouchDown && Math.abs(MyInputProcessor.dist)>5.0f)          
            handleSwipeInput();

这很完美,我在移动时在框架对象下方添加了一个对象数组(命名为圆圈),这样,这些元素数组也随着我的手指移动。

所以我在框架对象下面依次设置圆[]的位置:

if(parts.size()!=0)
        {
            for (int i = 0; i <parts.size(); i++){
            if(parts.get(i) instanceof Block){
           circles[i].setIndex(parts.get(i).getIndex()) ;

         circles[i].setPosition(frame.getX()-frame.getRadius(),
            (frame.getY()-(frame.getRadius()*3))-(60*i));
    }

这也有效 fine.Both 框架和下面的对象感觉它们正在随着我的手指移动,并以提到的速度框架对象。

现在我想创建这样的效果,每个圆圈对象都应该根据它们的位置跟随框架对象有一些延迟。

让它看起来像一条流畅的蛇运动(就像在蛇对方块游戏中一样)。

为此,我尝试使用补间。

  Tween.to(circles[0], Accessor.POS_XY,0.05f)
     .target(circles[0].getX()+10,circles[0].getY())
     .ease(TweenEquations.easeInSine)
     .start(tweenManager);
     Tween.to(circles[1], Accessor.POS_XY,0.05f)
     .target(circles[1].getX()+20,circles[1].getY())
     .ease(TweenEquations.easeInSine)
     .start(tweenManager);
     Tween.to(circles[2], Accessor.POS_XY,0.05f)
     .target(circles[2].getX()+30,circles[2].getY())
     .ease(TweenEquations.easeInSine)
     .start(tweenManager);

但我无法用补间来解决逻辑问题。

根据触摸输入,使用补间为每个圆形对象实现顺序延迟感到困惑。

我创建了一个包含顺序值的数组来控制圆圈的移动,使其看起来像一条蛇。

  private float delayValue[]={0.03f,0.04f,0.05f,0.06f,0.07f,0.08f,0.09f,0.10f};

我使用的简单补间:

 if (movedir > 0) {
                frame.setX(frame.getX() + actualSpeed);
                MyInputProcessor.dist -= actualSpeed;
                tweenManager.killAll();
                for (int i = 0; i < parts.size(); i++) {
                    Tween.to(circles[i], Accessor.POS_XY, delayValue[i])
                            .target(frame.getX() - frame.getRadius(), circles[i].getY()).start(tweenManager);
                }
            } else if (movedir < 0) {
                frame.setX(frame.getX() - actualSpeed);
                MyInputProcessor.dist += actualSpeed;
                tweenManager.killAll();
                for (int i = 0; i < parts.size(); i++) {
                    Tween.to(circles[i], Accessor.POS_XY, delayValue[i])
                            .target(frame.getX() - frame.getRadius(), 
             circles[i].getY()).start(tweenManager);

            }

设置位置:

  if(parts.size()!=0)
    {
        for (int i = 0; i <parts.size(); i++){
        if(parts.get(i) instanceof Block){
       circles[i].setIndex(parts.get(i).getIndex()) ;

        circles[i].setPosition(circles[i].getX(), (frame.getY() - 
     (frame.getRadius() * 3)) - (60 * i));
}

就这样,我的动作很流畅,达到了我的要求。(虽然比不上吃蛇方块,但是满足了我的要求)