标签:
教程总目录: http://blog.csdn.net/zqiang_55/article/details/50878524
上一篇我们介绍了Libgdx中舞台类,按照类的继承图,我们应该介绍Actor类,从截图中我们知道Libgdx中的UI控件都是继承自Actor。
前面我们也介绍过Sprite类,Actor有点类似于Sprite类,保存位置,大小,颜色,旋转中心,缩放以及Actions等,同时里面也包含了一个舞台类。Actor的坐标系(local Coordinate)从左下角开始计算
我们在初始化Actor时可以设置Listener,处理一些监听事件。
注意:1. Actor的draw函数默认是空的,需要重写。
2./** Called by the framework when this actor or any parent is added to a group that is in the stage.
* @param stage May be null if the actor or any parent is no longer in a stage. */
protected void setStage (Stage stage) {
this.stage = stage;
}
3. 此外还有一个函数需要注意:
/* Set bounds the x, y, width, and height. /
public void setBounds (float x, float y, float width, float height)
测试代码:
Stage stage;
MyActor myActor;
@Override
public void create() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
myActor = new MyActor();
yActor.setX(Gdx.graphics.getWidth() / 2 - myActor.getWidth() / 2);
myActor.setY(Gdx.graphics.getHeight() / 2 - myActor.getHeight() / 2);
stage.addActor(myActor);
}
@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() {
stage.dispose();
myActor.dispose();
}
public class MyActor extends Actor implements Disposable {
TextureRegion region;
public MyActor() {
region = new TextureRegion(new Texture("badlogic.jpg"));
setSize(this.region.getRegionWidth(), this.region.getRegionHeight());
// 设置监听函数,可以处理一些事情
addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
//点击图片,图片隐藏![这里写图片描述](http://img.blog.csdn.net/20160425220710777)
setVisible(false);
super.clicked(event, x, y);
}
});
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
batch.draw(region, getX(), getY(),
getOriginX(), getOriginY(),
getWidth(), getHeight(),
getScaleX(), getScaleY(),
getRotation());
}
@Override
public void dispose() {
region.getTexture().dispose();
}
}
标签:
原文地址:http://blog.csdn.net/zqiang_55/article/details/51245901