开源java游戏框架libgdx专题-15-系统控件-Button类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开源java游戏框架libgdx专题-15-系统控件-Button类相关的知识,希望对你有一定的参考价值。
Button类继承与Actor类,可以在舞台中使用,但是它也继承了许多Actor的子类,如Group、Table、WidgetGroup灯。
常用构造方法:
Button():创建按钮对象,不设置其样式及大小
Button(Button.ButtonStyle style):创建一个按钮,参数是指定按钮的大小及样式
Button(Drawable up):创建一个按钮对象,按钮未单击时(即抬起状态),样式为参数内纹理样式
Button(Drawable up,Drawable down):创建按钮对象,第一个参数代表抬起状态使用up纹理,第二个参数代表按下状态时使用down纹理
Button(Drawable up,Drawable down):创建按钮对象,第一二参数同上,第三个参数代表鼠标滑过按钮时使用的纹理
Button(Skin skin):创建按钮对象,使用Skin对象中的样式
Button(Skin skin,String styleName):使用Skin对象中指定名称的按钮样式。
常用方法:
draw(Batch batch,float parentAlpha):绘制当前按钮。第一个参数代表绘制所需的精灵画笔,第二个参数代表绘制的当前按钮透明度
getClickListener():获取当前按钮所添加的监听器
getMinHight():获取按钮最小高度
getMinWidth():获取按钮最小宽度
invalidate():作废当前布局
invalidateHierarchy():作废当前演员及其所有父类
layout():计算当前布局内所有演员的绘制信息
pack():排列当前演员的宽度与高度信息并选取最优的宽度与高度
setFillParent(boolean fillParent):是否填充满当前区域
setLayoutEnabled(boolean enabled):是否将当前布局使用与当前演员及其子类
validate():确定当前演员是否已被布局
常用属性:
checked:鼠标滑过按钮显式的纹理,返回类型为Drawable
checkedOver:鼠标滑过按钮并离开按钮显式的纹理对象,返回Drawable类型
down:被按钮按下时显示的纹理,返回Drawable类型
pressedOffsetX:单击当前按钮的鼠标的X轴偏移量
pressedOffsetY:单击当前按钮的鼠标的Y轴偏移量
unpressedOffsetX:离开当前按钮的鼠标的X轴偏移量
unpressedOffsetY:离开当前按钮的鼠标的Y轴偏移量
up:按钮未被按下时显示的纹理,返回类型为Drawable类型变量
代码:
1 package com.mygdx.syscontrol; 2 3 import com.badlogic.gdx.scenes.scene2d.InputEvent; 4 import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; 5 /** 6 * 我的点击事件类 7 * @author Jack(乐智) 8 * @blog dtblog.cn 9 * @qq 984137183 10 */ 11 public class MyClick extends ClickListener { 12 13 @Override 14 public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 15 //打印 16 System.out.println("Button:"+button); 17 return true; 18 } 19 20 }
1 package com.mygdx.syscontrol; 2 3 import com.badlogic.gdx.ApplicationAdapter; 4 import com.badlogic.gdx.Gdx; 5 import com.badlogic.gdx.graphics.GL20; 6 import com.badlogic.gdx.graphics.Texture; 7 import com.badlogic.gdx.graphics.g2d.TextureRegion; 8 import com.badlogic.gdx.scenes.scene2d.Stage; 9 import com.badlogic.gdx.scenes.scene2d.ui.Button; 10 import com.badlogic.gdx.scenes.scene2d.ui.Skin; 11 import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; 12 /** 13 * 使用Button 14 * @author Jack(乐智) 15 * @blog dtblog.cn 16 * @qq 984137183 17 */ 18 public class TestButton extends ApplicationAdapter { 19 //声明Skin对象 20 private Skin skin; 21 //声明按钮A 22 private Button buttonA; 23 //声明按钮B 24 private Button buttonB; 25 //声明舞台 26 private Stage stage; 27 //按钮抬起时纹理 28 private Texture btnUp; 29 //按钮按下时纹理 30 private Texture btnDown; 31 //声明监听器 32 private MyClick click; 33 34 @Override 35 public void create() { 36 //实例化skin对象 37 skin=new Skin(Gdx.files.internal("button/test.json")); 38 //实例化舞台 39 stage=new Stage(); 40 //初始化按钮抬起纹理 41 btnUp=new Texture(Gdx.files.internal("button/btnUp.png")); 42 //初始化按钮按下纹理 43 btnDown=new Texture(Gdx.files.internal("button/btnDown.png")); 44 //抬起纹理样式 45 TextureRegionDrawable btn_up=new TextureRegionDrawable(new TextureRegion(btnUp)); 46 //按下纹理样式 47 TextureRegionDrawable btn_down=new TextureRegionDrawable(new TextureRegion(btnDown)); 48 //实例化按钮A 49 buttonA=new Button(skin.get("style", Button.ButtonStyle.class)); 50 //初始化按钮B 51 buttonB=new Button(btn_up,btn_down); 52 //初识化监听器 53 click=new MyClick(); 54 //添加监听器 55 buttonA.addListener(click); 56 buttonB.addListener(click); 57 //设置按钮b的位置 58 buttonB.setPosition(100, 100); 59 //添加按钮b到舞台 60 stage.addActor(buttonB); 61 //添加按钮a到舞台 62 stage.addActor(buttonA); 63 //注册舞台监听 64 Gdx.input.setInputProcessor(stage); 65 } 66 67 @Override 68 public void render() { 69 Gdx.gl.glClearColor(1, 1, 1, 1); 70 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 71 //更新舞台逻辑 72 stage.act(); 73 //绘制舞台内容 74 stage.draw(); 75 } 76 77 }
资源路径:
![技术分享](C:\Users\Jack\AppData\Local\YNote\data\qq5551D6147D01DD9739CC6CA59CB01398\1e1d2e348a7344cfa0010b1d5f2ea1ca\clipboard.png)
![技术分享](http://www.dtblog.cn/wp-content/uploads/2016/10/clipboard-12.png)
效果图:
![技术分享](C:\Users\Jack\AppData\Local\YNote\data\qq5551D6147D01DD9739CC6CA59CB01398\240500f22286482abd51d1811d40856a\clipboard.png)
![技术分享](http://www.dtblog.cn/wp-content/uploads/2016/10/clipboard-14.png)
原文由博主 乐智 编辑撰写,版权归博主所有。
原文地址 http://www.dtblog.cn/1170.html 转载请注明出处!
以上是关于开源java游戏框架libgdx专题-15-系统控件-Button类的主要内容,如果未能解决你的问题,请参考以下文章
开源java游戏框架libgdx专题-14-系统控件-Skin类
开源java游戏框架libgdx专题-01-libgdx介绍
开源java游戏框架libgdx专题-04-接口介绍及生命周期