Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

java - Add Actions.scaleTo() to a Label in LibGDX

In LibGDX, I want to make a text animation for my game. Therefore, I want that my labels gets larger with time. But if I use the scaleTo() method, nothing happens whereas other Actions like moveTo() work fine.

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

How can I make my labels scale? Thank you in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For performance reason most scene2d.ui groups have transform set to false by default.

For more detail you can check
https://github.com/libgdx/libgdx/wiki/Scene2d.ui#rotation-and-scale

If you want to scale, you can use Container which is useful for setting the size and alignment of a single widget.

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

Add your Action on container instead of Label.

container.addAction(Actions.parallel(Actions.moveTo(500, 300, 2.0f),Actions.scaleTo(0.1f, 0.1f,2.0f)));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...