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(); }
相关文章推荐
- Android使用libgdx实现模拟方向键控制角色移动的方法
- Libgdx之封装简单的脚本机制
- Libgdx学习笔记:UI之技能冷却按钮
- Android中AlertDialog的使用小结
- libgdx html 跨平台的坑
- gwt中 java与js 的相互调用
- libGDX引擎之存入数据到keychain(IOS)
- 二,游戏的game loop搭建
- 三,资源初始化操作和主菜单界面的创建
- 四,主要菜单界面的创建
- 五,一个可以左右滑动的关卡选择界面
- 六,游戏主界面
- 七,游戏世界
- 八,游戏世界的渲染
- 九,碰到的问题
- 学习Libgdx游戏开发(二)
- 学习Libgdx游戏开发(一)
- libgdx 自动适应分辨率的方法
- libgdx学习笔记02