libgdx中绘制的解释

时间:2014-10-05 12:49:06

标签: java libgdx

有人可以告诉我在libgdx中调用draw方法是什么意思吗?

@Override
public void draw (Batch batch, float parentAlpha) {
//whatever goes here
}

我不知道我是否可以很好地解释这一点,但我的问题是,在实施了draw方法之后,谁经常调用它?在Actor类中,例如:

public void draw (Batch batch, float parentAlpha) {
}

我猜,虽然我不确定,但某些代码负责调用每个渲染或某些东西。

1 个答案:

答案 0 :(得分:0)

简答.-

Stage调用其所有Actors的Draw方法。

长期答案只是为了完整性.-

阶段调用其(根)组的Draw方法。

Stage.java -L118

public void draw () {
    Camera camera = viewport.getCamera();
    camera.update();

    if (!root.isVisible()) return;

    Batch batch = this.batch;
    if (batch != null) {
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        root.draw(batch, 1);
        batch.end();
    }

    if (debug) drawDebug();
}

root group调用drawChildren方法。

Group.java -L56

public void draw (Batch batch, float parentAlpha) {
    if (transform) applyTransform(batch, computeTransform());
    drawChildren(batch, parentAlpha);
    if (transform) resetTransform(batch);
}

这是调用所有子Actors绘制方法的方法。

Group.java -L66

protected void drawChildren (Batch batch, float parentAlpha) {
    parentAlpha *= this.color.a;
    SnapshotArray<Actor> children = this.children;
    Actor[] actors = children.begin();
    Rectangle cullingArea = this.cullingArea;
    if (cullingArea != null) {
        // Draw children only if inside culling area.
        float cullLeft = cullingArea.x;
        float cullRight = cullLeft + cullingArea.width;
        float cullBottom = cullingArea.y;
        float cullTop = cullBottom + cullingArea.height;
        if (transform) {
            for (int i = 0, n = children.size; i < n; i++) {
                Actor child = actors[i];
                if (!child.isVisible()) continue;
                float cx = child.x, cy = child.y;
                if (cx <= cullRight && cy <= cullTop && cx + child.width >= cullLeft && cy + child.height >= cullBottom)
                    child.draw(batch, parentAlpha);
            }
        } else {
            // No transform for this group, offset each child.
            float offsetX = x, offsetY = y;
            x = 0;
            y = 0;
            for (int i = 0, n = children.size; i < n; i++) {
                Actor child = actors[i];
                if (!child.isVisible()) continue;
                float cx = child.x, cy = child.y;
                if (cx <= cullRight && cy <= cullTop && cx + child.width >= cullLeft && cy + child.height >= cullBottom) {
                    child.x = cx + offsetX;
                    child.y = cy + offsetY;
                    child.draw(batch, parentAlpha);
                    child.x = cx;
                    child.y = cy;
                }
            }
            x = offsetX;
            y = offsetY;
        }
    } else {
        // No culling, draw all children.
        if (transform) {
            for (int i = 0, n = children.size; i < n; i++) {
                Actor child = actors[i];
                if (!child.isVisible()) continue;
                child.draw(batch, parentAlpha);
            }
        } else {
            // No transform for this group, offset each child.
            float offsetX = x, offsetY = y;
            x = 0;
            y = 0;
            for (int i = 0, n = children.size; i < n; i++) {
                Actor child = actors[i];
                if (!child.isVisible()) continue;
                float cx = child.x, cy = child.y;
                child.x = cx + offsetX;
                child.y = cy + offsetY;
                child.draw(batch, parentAlpha);
                child.x = cx;
                child.y = cy;
            }
            x = offsetX;
            y = offsetY;
        }
    }
    children.end();
}