将Actions.scaleTo()添加到LibGDX中的标签

eri*_*ers 2 java android libgdx

在LibGDX中,我想为游戏制作文本动画。因此,我希望我的标签随着时间的推移而变大。但是,如果我使用该scaleTo()方法,则什么也不会发生,而其他动作之类的就moveTo()可以正常工作。

label1 = new Label("Test text", new Label.LabelStyle(font, Color.BLACK));
label2.addAction(Actions.parallel(Actions.moveTo(500, 300, 2.0f),Actions.scaleTo(0.1f, 0.1f,2.0f)));

label2 = new Label("Test text 2", new Label.LabelStyle(font, Color.BLACK));
label2.addAction(Actions.parallel(Actions.moveTo(500, 300, 2.0f),Actions.scaleTo(0.1f, 0.1f,2.0f)));

stage.addActor(label1);
stage.addActor(label2);
Run Code Online (Sandbox Code Playgroud)

如何使我的标签缩放?先感谢您!

Ary*_*yan 6

出于性能原因,大多数scene2d.ui组默认将transform设置为false。

有关更多详细信息,请检查
https://github.com/libgdx/libgdx/wiki/Scene2d.ui#rotation-and-scale

如果要缩放,可以使用“容器”,它对于设置单个小部件的大小和对齐方式很有用。

private Container<Label> container;

@Override
public void create() {
    stage=new Stage();

    Label label1 = new Label("Test text", new Label.LabelStyle(font, Color.BLACK));

    container=new Container<Label>(label1);
    container.setTransform(true);   // for enabling scaling and rotation
    container.size(100, 60);
    container.setOrigin(container.getWidth() / 2, container.getHeight() / 2);
    container.setPosition(100,200);
    container.setScale(3);  //scale according to your requirement

    stage.addActor(container);
}

@Override
public void render() {
    super.render();

    Gdx.gl.glClearColor(1,1,1,1);
    gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    stage.draw();
    stage.act();
}
Run Code Online (Sandbox Code Playgroud)

将操作添加到容器而不是标签上。

container.addAction(Actions.parallel(Actions.moveTo(500, 300, 2.0f),Actions.scaleTo(0.1f, 0.1f,2.0f)));
Run Code Online (Sandbox Code Playgroud)