【问题标题】:Removing animation logic from Render method in Libgdx从 Libgdx 中的 Render 方法中删除动画逻辑
【发布时间】:2024-01-09 23:45:01
【问题描述】:

我想将动画委托给不同的类/实体,以便渲染方法不会因动画所需的代码而臃肿。如果动画正在进行渲染方法应该执行该动画并在动画完成后移动到其正常处理。动画应该除了动画中涉及的必需精灵。 render 方法应该以正常方式渲染其他精灵。 (我说的是几个精灵的动画,而不是精灵表的精灵动画)

有没有面向对象的方式来实现这一点?

@Override
public void render ()
{

    long ctime = System.currentTimeMillis();
    Gdx.gl.glClearColor(backColor.r, backColor.g, backColor.b, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    float deltaTime = Gdx.graphics.getDeltaTime();
    if(!startGame)
        return;

    if(camera != null)
        batch.setProjectionMatrix(camera.combined);

batch.begin();
        for (int i = 0; i < clusterList.size(); i++) {
            clusterList.get(i).render(batch);

batch.end();
        if(!isFinished)
            scoreBar.setDeltaTime(deltaTime);

        Gdx.gl.glEnable(GL20.GL_BLEND);
        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
        shapeRenderer.setProjectionMatrix(batch.getProjectionMatrix());
        shapeRenderer.setColor(Color.GRAY.r,Color.GRAY.g, Color.GRAY.b, 0.6f);
        shapeRenderer.rect(0, camH - barHeight, camW, barHeight);
        shapeRenderer.end();
        Gdx.gl.glDisable(GL20.GL_BLEND);

        batch.begin();
        scoreBar.render(batch);


        if (animateSpread) {
            timeLoopSpread += deltaTime;
            if (timeLoopSpread > duration) {
                timeLoopSpread = 0;
                animateSpread = false;
            } else
                animateCluster();
        }


}

private void animateCluster() {
        float factor = Interpolation.pow5Out.apply(timeLoopSpread /duration);
        for (int index =0; index < clusterList.size(); index++) {
            Cluster tempC = clusterList.get(index);
            currentPos.set(tempC.endPos);
            currentPos.sub(tempC.startPos);
            currentPos.scl(factor);
            currentPos.add(tempC.startPos);
            tempC.setCenterPosition(currentPos);
        }
    }

【问题讨论】:

    标签: android animation libgdx


    【解决方案1】:

    做到这一点的面向对象的方法是让集群自己动画。 而不是调用AnimateClusters(),您可以启用/禁用它们的动画。无论您在哪里启用animateSpread,您都会调用类似

    for (int i = 0; i < clusterList.size(); i++) {
        clusterList.get(i).spreadStart();
    }
    

    然后更新render()中的每个集群,每个集群都会保持自己的动画时间。

    for (int i = 0; i < clusterList.size(); i++) {
        // update animation in there
        clusterList.get(i).update(delta);
    }
    

    您也可以像开始动画一样停止动画。

    【讨论】:

    • 你的意思是在渲染方法中这样做吗?如果我这样做,当我添加更多动画时,它会使渲染方法更加臃肿。是否可以在渲染中像 Animation.Spread(clusterlist) 那样做,以便整个动画逻辑在 Animation calss 中
    • 它怎么比你已经拥有的更臃肿?可能你误解了我在里面的评论,不是直接把动画代码贴在循环里,而是在Cluster类的update()方法中