【问题标题】:LibGdx - Adding array of actors to a table, with delayLibGdx - 将演员数组添加到表中,延迟
【发布时间】:2016-08-02 19:22:48
【问题描述】:

我希望有几颗彗星落在我的 UI 背景中,我有一个工作的彗星 Actor 可以完成它应该做的事情,但我不知道如何用这些彗星创建一个连续的产卵(随机延迟在每个)之间)在一个表中,没有scene2d/actors,它看起来像:

cometTimer += delta
if(cometTimer >= interval){
    addCometToArray();
    cometTimer = 0;
}

cometArray 被循环遍历并在每一帧中绘制,然后在实体超出范围时将其移除。

我知道如何将 Actors 添加到表中的唯一方法是这样的:

table().add(new DialogComet());

如何使用 Scene2d 添加此类行为?

【问题讨论】:

  • Table extends Group,它可以为您提供表中的 Actor,就像一个数组 (libGDX SnapshotArray)。因此,无论如何您都可以对代码使用类似的逻辑。将彗星添加到表中,如果您想遍历彗星(查看它们是否超出范围、设置位置等),请使用 drawChildren 或 getChildren 等方法,然后在 OOB 时使用 tableVar.removeActor(specificComet)。跨度>
  • @PeterR 我一直在尝试实现此功能,但无法使其正常工作,您可以在答案中编写一些示例代码吗?
  • 我会尝试,但可能需要几天/几天才能完成。
  • 这几天忙于工作和旅行,但我发布了一个答案。可能离基地很远,但希望这就是你所询问的方向。

标签: libgdx


【解决方案1】:

不确定这是否是您要找的,但下面是一个小型工作应用程序,它显示彗星从上到下“下落”,使用表格并让表格管理彗星(没有单独的数组/数据结构)。我创建了一个小型 Comet 类,它也扩展了 Actor,以允许移动和放置。

“主”类:

import java.util.Iterator;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Table;

public class StageComet  implements ApplicationListener {

    private static final float INTERVAL = 0.3f;

    private Batch batch;
    private ShapeRenderer shapeRenderer;
    private OrthographicCamera camera;
    private BitmapFont font;

    private Table rootTable;
    private Table cometTable;
    private Stage stage;
    private Iterator<Actor> iter;
    private Comet comet;

    private float cometTimer = 0;
    private float delta = 0;

    @Override
    public void create() {

        camera = new OrthographicCamera();
        camera.setToOrtho(false, 960, 640);

        shapeRenderer = new ShapeRenderer();
        batch = new SpriteBatch();
        font = new BitmapFont();

        stage = new Stage();

        /*
         * The root table could contain main "play" actors. It is empty in this example.
         */
        rootTable = new Table();
        rootTable.setFillParent(true);

        /*
         * Usually in Scene2d I think the practice is only to have 1 root table that takes up the entire screen (above), 
         * but for simplicity/illustrative purposes, I created a cometTable only, set it to Fill Parent as well, and the
         * getChildren() of the table will have our array of comets in play at any given time.
         */
        cometTable = new Table();
        cometTable.setFillParent(true);

        stage.addActor(rootTable);
        stage.addActor(cometTable);
    }


    @Override
    public void render() {
        Gdx.gl.glClearColor(0, 0, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        camera.update();
        batch.setProjectionMatrix(camera.combined);

        delta = Gdx.app.getGraphics().getDeltaTime();
        stage.act(delta);                                               // make sure the comets "fall"

        shapeRenderer.begin(ShapeType.Filled);                          // simple rendering of comets, they are just a circle ...
        iter = cometTable.getChildren().iterator();                     // Table subclasses Group, which has a snapshot array of its Actors 
        while ( iter.hasNext() ) {
            comet = (Comet)iter.next();

            shapeRenderer.circle(comet.getX(), comet.getY(), 20.0f);    // Draw the comet

            if ( comet.getY() < -100 ) {                                // Hack/hardcode, if the comet fell far enough "off stage" ...
                iter.remove();                                          // ... remove it from the stage
            }
        }
        shapeRenderer.end();

        /*
         * Sample code from original question on how to create a comet without scene2d ...
         */
        cometTimer += delta;
        if ( cometTimer > INTERVAL ) {
            cometTable.add(new Comet());                                // ... but in this case, we use scene2d
            cometTimer = 0;
        }

        /*
         * To keep track, display a simple message of # of comets on stage at any given time.
         */
        batch.begin();
        font.draw(batch, "Comets on stage: " + cometTable.getChildren().size, 100, 100);
        batch.end();
     }

    /*
     * I may have missed disposing something, but you get the idea ...
     */
    @Override
    public void dispose() {
        shapeRenderer.dispose();
        batch.dispose();
        stage.dispose();
        font.dispose();
    }

    @Override
    public void resize(int width, int height) {  }

    @Override
    public void pause() { }

    @Override
    public void resume() { }
}

还有彗星小班:

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.Actor;

public class Comet extends Actor{

    /*
     * Spawn a comet at the top of the screen, in the middle
     */
    public Comet() {
        super();
        this.setY(Gdx.app.getGraphics().getHeight());
        this.setX(Gdx.app.getGraphics().getWidth()/2.0f);
    }

    /*
     * Let the comet fall (same speed) to the bottom of the screen ...
     */
    @Override
    public void act (float delta) {
        this.setY(this.getY() - 10);
        super.act(delta);
    }

}

【讨论】:

  • 我找到了解决我的问题的另一种方法,但会接受并给予赏金,因为这似乎是另一种可行的解决方案,感谢您的帮助!
猜你喜欢
  • 2020-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-06
  • 2020-10-22
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
相关资源
最近更新 更多