LibGDX游戏引擎-6-常用控件(Component)

qsuron 发布于 2014-03-17 libGDX框架 56 次阅读 无~ 2037 字 预计阅读时间: 9 分钟


一丶标签(Lable)

Label(java.lang.CharSequence text, Label.LabelStyle style)
Label(java.lang.CharSequence text, Skin skin)

-------------------------------------------------------------------------------------

使用前:

首先你需要创建一个hiero的.fnt和.png文件(如何创建请看土豆的教程三),
例如土豆就是创建的“Potato.fnt”“Potato.png”,然后传入“Potato.fnt”和“Potato.png”
和“Color”就构成了一个LabelStyle。

-------------------------------------------------------------------------------------

 

使用方法:

BitmapFont font = new BitmapFont(fnt,png,是否旋转);
LabelStyle style = new LabelStyle(font,font.getColor());

这样一个LabelStyle就实现了,下面就可使用它new出Label

Stage stage = new Stage(WIDTH,HEIGHT,true);//宽 高 锁定比率
Label label = new Label("小树",labelStyle);
label.setPosition(10,200);//设置坐标
label.setScale(2f);//设置显示倍数
stage.addActor(label);

在render方法中

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

 通过API可以看出,前面的参数是一个字符串,后面是一个lableStyle,然后第二个lable参数设置,
第一个是字符串,第二个是一个skin类型的参数。lable自带了一些方法,
比如设置旋转、拉伸、位置、大小等。由于lable控件是属于actor类,
所以应该加入到舞台中去展示出来,关于舞台我会在后面详细给大家讲解,这里简单用一下舞台。

代码:

package com.qsuron;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;

public class MyDemo implements ApplicationListener{
private SpriteBatch batch;
private BitmapFont font;
private LabelStyle labelStyle;
private Stage stage;
private Label label;

@Override
public void create() {

int WIDTH = Gdx.graphics.getWidth();
int HEIGHT = Gdx.graphics.getHeight();
batch = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("data/test.fnt"),Gdx.files.internal("data/test.png"),false);
labelStyle = new LabelStyle(font,font.getColor());
stage = new Stage(WIDTH,HEIGHT,true);//宽 高 锁定比率
label = new Label("小树",labelStyle);
label.setPosition(10,200);//设置坐标
label.setScale(2f);//设置显示倍数
stage.addActor(label);
}

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

@Override
public void render() {

Gdx.gl.glClearColor(0,0,0, 0);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}

@Override
public void resize(int width, int height) {
}

@Override
public void pause() {
}

@Override
public void resume() {
}

}