标签:
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 }
【开源java游戏框架libgdx专题】-15-系统控件-Button类
标签:
原文地址:http://www.cnblogs.com/AIThink/p/5936307.html