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

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


libgdx将每一个可以实现一定动作和效果的东西,全部定义为演员。同时libgdx也提供了一些自带的演员,本篇文章就这些常用的控件,或者说是演员,来做一个粗略的了解!

例如:标签,按钮,勾选框,下拉框,图片,输入框,列表,滑动面板,滑条,分割面板等等,这些都是演员,都是可以加入到舞台类中的,舞台这里先不做详细介绍,因为后面我会单独的给大家讲解下舞台类。演员类可以分为libgdx提供给我们的演员类,还有就是我们自己继承Actor然后自己写的演员类,

下面我就针对这2中分类给大家介绍下libgdx中的演员类。

列举 Label Image ImageButton这三个,分别是文字,图片,图片按钮  , 实例代码如下:

package com.qsuron;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class MyDemo implements ApplicationListener{
private SpriteBatch batch;
private BitmapFont font;
private LabelStyle labelStyle;
private Stage stage;
private Label label;
private Image image;
private ImageButton imageButton;
private TextureRegionDrawable up = null;
private TextureRegionDrawable down = null;
private TextureRegionDrawable press = null;

@Override
public void create() {

int WIDTH = Gdx.graphics.getWidth();
int HEIGHT = Gdx.graphics.getHeight();
batch = new SpriteBatch();
//label
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);
//image
image = new Image(new Texture(Gdx.files.internal("data/pic2.jpg")));
image.setPosition(200,0);//绘画起点
image.setSize(320,240);//绘画大小
image.setScale(1.0F); //缩放比例
image.setOrigin(0,0); //设置旋转中心
image.setRotation(30); //旋转角度
stage.addActor(image);
//imageButton
TextureRegion tr[][] = TextureRegion.split(
new Texture(Gdx.files.internal("data/ImageButton.png")),100,100);
up = new TextureRegionDrawable(tr[0][0]);
down = new TextureRegionDrawable(tr[0][1]);
imageButton = new ImageButton(up,down);
imageButton.setPosition(100, 100);
stage.addActor(imageButton);
Gdx.input.setInputProcessor(stage);//让舞台接收输入
}

@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() {
}

}

下面为大家介绍这三个常用演员的使用方法: