libGDX向SpriteBatch添加自定义方法

时间:2015-10-18 20:47:02

标签: java libgdx

我正在尝试将自己的自定义draw()函数添加到libGDX中的SpriteBatch类,但SpriteBatch类是在jar文件中编译的。

我曾尝试创建自己的扩展SpriteBatch类的类,但我需要访问只能在类本身中访问的私有变量...

我认为我需要下载libGDX源码,修改我需要的内容并重新编译,但我在this tutorialthis question之后遇到了一些麻烦,此外按照这种方法在Mac上安装Ant(Mac早在2011年)。

我应该如何修改和应用新的源代码?

2 个答案:

答案 0 :(得分:0)

Stage.draw ()中包含的所有内容都可以使用Stage类中包含的公共getter进行检索。

从舞台:

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

可以写成:

public void draw () {
    Camera camera = stage.getCamera();
    Group root = stage.getRoot();
    Batch batch = stage.getBatch();
    camera.update();

    if (!root.isVisible()) return;

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

    if (root.getDebug()) super.drawDebug();
}

我在这里拍摄时,我目前无法测试此代码。如果一切顺利,您应该能够编辑该绘制方法以满足您的需求。

答案 1 :(得分:0)

最后,我只是简单地从here复制了整个SpriteBatch.java源代码,并创建了自己的SprBatch.java(不应该与SpriteBatch同名,否则在运行时会出错) )带有复制代码的类以及修改,并使用该类代替SpriteBatch进行所有draw()调用。

因为我在Batch中添加了一个覆盖draw()的函数,而SpriteBatch为draw方法扩展了Batch,我还需要创建一个复制的Batch类(在我的例子中为SBatch)。并且让SprBatch改为扩展SBatch。

使用我复制的SpriteBatch和Batch修改代码可以正常工作。

注意:向SprBatch添加需要@Overidde的函数(例如draw())时,请务必将函数构造函数添加到复制的Batch类中。

最后,它在项目中的表现如何(Eclipse):

Project Explorer

要使用它,请像SpriteBatch一样使用它......

Import

Declare