libGdxでグループを作成する方法は?


8

libGdxでグループを使用する方法について簡単な例を示していただけますか?したがって、親を移動すると、すべての子が一緒に移動し、回転とスケーリングも同じようになります。

オンラインでチュートリアルと例を探しましたが、libGdxの最新バージョンがStageクラスにいくつかの変更を加えたようです。

ありがとうございました


stage2dを使用するか、ゼロから作成するか?
Gustavo Maciel 14年

stage2dを使用して私は推測します:p?それはより良い方法ですよね?
アラスリップノット2014年

ベストは主観的です。私にとっては、一から作るのが良いです(私はそれを十分に制御できるため)。他の人にとっては、stage2Dの方が良いので、あまり気にする必要はありません。 Stage2Dの機能。
Gustavo Maciel 14年

とにかく、私はStage2Dでそれを作成する方法がわかりません:P
グスタボMaciel

笑問題ありません私は今解決策を見つけました^^ thnxとにかく:D
alaslipknot

回答:


5

これが私が必要とするものです。このクラスでは、3つの画像を作成し、それらを単一のオブジェクトとして回転することができます。入力ありがとうございます。

public class LearnGdx extends ApplicationAdapter {

    public static final int WIDTH = 800;
    public static final int HEIGHT = 480;

    private Stage stage;
    private Group group;

    private float rotSpeed = 5;

    @Override
    public void create() {

        // Create a stage
        stage = new Stage(new StretchViewport(WIDTH, HEIGHT));

        // Create a group and add it to the stage.
        group = new Group();
        stage.addActor(group);

        // Create images and add them to the group.
        final Texture region = new Texture(Gdx.files.internal("circle.png"));
        Image img = new Image(region);
        Image img2 = new Image(region);
        Image img3 = new Image(region);

        img2.setColor(new Color(1, 0, 0, 1));
        img3.setColor(new Color(0, 0, 1, 1));

        group.addActor(img2);
        group.addActor(img3);
        group.addActor(img);

        // Images are positioned relative to the group...
        img.setPosition(0, 0);
        img2.setPosition(img.getWidth()/2, 0);
        img3.setPosition(-img.getWidth()/2, 0);

        // Group is positioned relative to the stage...
        group.setPosition(WIDTH / 2 - img.getWidth() / 2,
            HEIGHT / 2 - img.getHeight() / 2);
        group.setOrigin(img.getWidth()/2,img.getHeight()/2);

    }

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

        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();

        if (Gdx.input.isKeyPressed(Keys.LEFT)) {
            group.rotateBy(rotSpeed);
        }
        if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
            group.rotateBy(-rotSpeed);
        }
    }
}
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.