您的位置:首页 > 其它

Libgdx之Label Image

2016-05-18 21:20 375 查看

Label类

标签类,里面包裹了文字,可以定义文字显示的位置,排列方式等。主要在游戏中显示时间,分数等信息。

不知道些点啥就来个类图吧



注意: Libgdx中字体缩放是基于像素的,因此在使用Label是如果过度的缩小的Label或者是缩放字体时,绘制的文字会消失。因此最好Label是基于GUI坐标

Image类

注意: 如果你使用下面初始化方法:

image = new Image();

image.setDrawable(new TextureRegionDrawable(new TextureRegion(texture)));

要记得setSize或者setWidth/setHeight

image.setSize(image.getPrefWidth(), image.getPrefHeight());

否则由于width=height=0 image虽然绘制了但并不在屏幕上显示出来

下面是测试代码,注意看一下注释:

Texture texture;
Image image;
Label label;

Skin skin;
Stage stage;
int num;

@Override
public void create() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);

texture = new Texture("badlogic.jpg");
//      image = new Image(texture);
image = new Image();
image.setDrawable(new TextureRegionDrawable(new TextureRegion(texture)));
image.setSize(image.getPrefWidth(), image.getPrefHeight());
image.setPosition(20, 20);
//      将Image铺满屏幕
//      image.setFillParent(true);
Gdx.app.log("TAG", "img width=" + image.getWidth() + "img preWidth=" + image.getPrefWidth());
image.addListener(new ActorGestureListener() {

@Override
public void tap(InputEvent event, float x, float y, int count, int button) {
image.setScale(0.5f);
// 通过 此处的log可以看出缩放之后width和preWidth都不会发生变化
Gdx.app.log("TAG", "img width=" + image.getWidth() + "img preWidth=" + image.getPrefWidth());
}

});
stage.addActor(image);

skin = new Skin(Gdx.files.internal("uiskin.json"));
label = new Label("click tap", skin);
Gdx.app.log("TAG", "width=" + label.getWidth() + "preWidth=" + label.getPrefWidth());
label.setPosition(image.getX(), image.getY() + image.getHeight() + 10f);
label.addListener(new ActorGestureListener() {

@Override
public void tap(InputEvent event, float x, float y, int count, int button) {
num++;
label.setText("click tap" + num);
// 通过Log可以看出width就是初始化时候大小,随着字体的变化width不会变化,但是prewidth会发生变化
Gdx.app.log("TAG", "width=" + label.getWidth() + "preWidth=" + label.getPrefWidth());
}

});
stage.addActor(label);
}

@Override
public void render() {
Gdx.gl.glClearColor(0.39f, 0.58f, 0.92f, 1.0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

stage.act();
stage.draw();
}

@Override
public void dispose() {
texture.dispose();
skin.dispose();
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  libgdx