Libgdx之Group

Libgdx之Group

Group可以说Libgdx中非常好用的一个组件,可以把其余的Actor封装在一起,然后赋予同样的Actions或者Position等。
Group的坐标是从左下角开始的,如果将设置Group.setPosition(0,0) 那么就是从屏幕左下角开始。

下面的示例是从一篇英文博客中找到的,就直接把代码拿过来用了。其实在统一设置一些演员的属性时,我们可以把它们都封装在Group里面
从下面代码中我们可以知道,当我们旋转Group的时候2个演员是同时在选中的。
这里写图片描述Libgdx之Group_第1张图片

下面是测试代码:

private Stage stage;
    private Group group;

    @Override
    public void create() {
        stage = new Stage();
        final TextureRegion jetTexture = new TextureRegion(new Texture("jet.png"));
        final TextureRegion flameTexture = new TextureRegion(new Texture("flame.png"));

        final Actor jet = new Actor() {
            public void draw(Batch batch, float alpha) {
                batch.draw(jetTexture, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), getScaleX(),
                        getScaleY(), getRotation());
            }
        };
        jet.setBounds(jet.getX(), jet.getY(), jetTexture.getRegionWidth(), jetTexture.getRegionHeight());

        final Actor flame = new Actor() {
            public void draw(Batch batch, float alpha) {
                batch.draw(flameTexture, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(),
                        getScaleX(), getScaleY(), getRotation());
            }
        };
        flame.setBounds(0, 0, flameTexture.getRegionWidth(), flameTexture.getRegionHeight());
        flame.setPosition(jet.getWidth() - 25, 25);

        group = new Group();
        group.addActor(jet);
        group.addActor(flame);

        group.addAction(parallel(moveTo(200, 0, 5), rotateBy(90, 5)));

        stage.addActor(group);

    }

    @Override
    public void dispose() {
        stage.dispose();
    }

    @Override
    public void render() {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }

Libgdx之Group_第2张图片

你可能感兴趣的:(Libgdx,Libgdx教程,libgdx)