首页 文章

Tween创建一个对象的效果遵循另一个-LibGdx

提问于
浏览
1

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

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

这也很好 . 框架和下面的物体都会感觉到它们正在用我的手指移动,并且框架物体具有上述速度 .

现在我想创建一个类似的效果,每个圆形对象应根据它们的位置在帧对象后面有一些延迟 .

因此它看起来像一个平滑的蛇运动(如蛇与块游戏) .

为此,我尝试使用补间 .

enter image description here

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

但我无法使用补间来处理逻辑 .

根据触摸输入,使用补间实现每个圆形对象的顺序延迟 .

1 回答

  • 0

    我创建了一个包含连续值的数组,以控制圆圈看起来像蛇的运动 .

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

    通过这种方式,我设法按照我的要求顺利运动 . (虽然它不像蛇与块游戏那么好,但符合我的要求) .

相关问题