在libGDX中实现鼠标焦点影响按钮图标


创建方法(在render里面使用)

void  chu_pen(Rect_a rect){
		int x=Gdx.input.getX();
		int y=Gdx.graphics.getHeight() - Gdx.input.getY();

		if (x > rect.x && x < rect.x + rect.width && y > rect.y &&y < rect.y + rect.height) {
			btn.setStyle(style1);
		}
		else {
			btn.setStyle(style);
		}
     }

Rect_a类(java类可以用内部类)

private class Rect_a {
		public int x;
		public int y;
		public int width;
		public int height;
		Rect_a(int x, int y, int width, int height){
			this.x=x;
			this.y=y;
			this.width=width;
			this.height=height;
		}

将输入设置为舞台(写creat里面)

// 将输入处理设置到舞台(必须设置, 否则点击按钮没效果)
		Gdx.input.setInputProcessor(stage);

更新一个方法,调用按钮自带的over函数就可以实现。(后来看源码的时候发现的)


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注