LibGDX_6.3: 常用系统控件: 按钮(Button)

本文链接: http://blog.csdn.net/xietansheng/article/details/50187861

LibGDX 基础教程(总目录)

1. 按钮(Button)

按钮是游戏开发中常用的控件,一个按钮一般可分为两种状态,按下状态和弹起状态,可以使用两张图片分别来表示。按钮还通常与事件监听一起使用,用于触发某一事件(开始游戏,暂停游戏,发射子弹等)。

这里需要使用的下面两张按钮图片:

button_1.png button_2.png

这两张图片分别代表按钮的弹起和按下状态,把这两张图片保存到本地,重命名为 button_1.pngbutton_2.png,然后复制到 assets 资源文件夹中。

2. 代码示例: Button 的使用

package com.libgdx.test;
		
import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.StretchViewport;
	
/**
 * 游戏主程序的启动入口类
 */
public class MainGame extends ApplicationAdapter {
	
	private static final String TAG = MainGame.class.getSimpleName();
	
	// 视口世界的宽高统使用 480 * 800, 并统一使用伸展视口(StretchViewport)
	public static final float WORLD_WIDTH = 480;
	public static final float WORLD_HEIGHT = 800;
	
	// 舞台
	private Stage stage;
	
	// 按钮 弹起 状态的纹理
	private Texture upTexture;
	
	// 按钮 按下 状态的纹理
	private Texture downTexture;
	
	// 按钮
	private Button button;
	
	@Override
	public void create() {
		// 设置日志输出级别
		Gdx.app.setLogLevel(Application.LOG_DEBUG);
		
		// 使用伸展视口(StretchViewport)创建舞台
        stage = new Stage(new StretchViewport(WORLD_WIDTH, WORLD_HEIGHT));
        
        // 将输入处理设置到舞台(必须设置, 否则点击按钮没效果)
        Gdx.input.setInputProcessor(stage);
        
        /*
		 * 第 1 步: 创建 弹起 和 按下 两种状态的纹理
		 */
		upTexture = new Texture(Gdx.files.internal("button_1.png"));
		downTexture = new Texture(Gdx.files.internal("button_2.png"));
		
		/*
		 * 第 2 步: 创建 ButtonStyle
		 */
		Button.ButtonStyle style = new Button.ButtonStyle();
		
		// 设置 style 的 弹起 和 按下 状态的纹理区域
		style.up = new TextureRegionDrawable(new TextureRegion(upTexture));
		style.down = new TextureRegionDrawable(new TextureRegion(downTexture));
		
		/*
		 * 第 3 步: 创建 Button
		 */
		button = new Button(style);
		
		// 设置按钮的位置
		button.setPosition(100, 200);
		
		// 给按钮添加点击监听器
		button.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				Gdx.app.log(TAG, "按钮被点击了");
			}
		});
        
		/*
		 * 第 4 步: 添加 button 到舞台
		 */
        stage.addActor(button);
	}

	@Override
	public void render() {
		// 黑色清屏
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		// 更新舞台逻辑
		stage.act();
		// 绘制舞台
		stage.draw();
	}

	@Override
	public void dispose() {
		// 应用退出时释放资源
		if (upTexture != null) {
			upTexture.dispose();
		}
		if (downTexture != null) {
			downTexture.dispose();
		}
		if (stage != null) {
			stage.dispose();
		}
	}

}

运行代码,点击按钮,查看控制台日志的输出:

sy_result.png

sy_result-log.png


  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谢TS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值