三丶按钮(Button)-------------------------------------------------------------------------------------
ImageButton(Drawable imageUp, Drawable imageDown, Drawable imageChecked) //弹起 按下 按击
ImageButton(Skin skin)
-------------------------------------------------------------------------------------
Drawable类:在已知的一个给定的矩形内,绘制本身。它提供了边框的大小和最小尺寸,
通过它自带的方法,可以确定的大小和位置。其实他就是为了image提供一个矩形区域,
这里大家不要搞混淆,他只是提供区域,但是没有规定一定button必须画成矩形。
我们使用下面这组图片作为button的image,这也是在网上随便找的
-------------------------------------------------------------------------------------
ImageButton(Drawable imageUp, Drawable imageDown, Drawable imageChecked) //弹起 按下 按击
ImageButton(Skin skin)
-------------------------------------------------------------------------------------
Drawable类:在已知的一个给定的矩形内,绘制本身。它提供了边框的大小和最小尺寸,
通过它自带的方法,可以确定的大小和位置。其实他就是为了image提供一个矩形区域,
这里大家不要搞混淆,他只是提供区域,但是没有规定一定button必须画成矩形。
我们使用下面这组图片作为button的image,这也是在网上随便找的
-------------------------------------------------------------------------------------
使用代码如下:
TextureRegion tr[][] = TextureRegion.split( ew Texture(Gdx.files.internal("data/ImageButton.png")),100,100); up = new TextureRegionDrawable(tr[0][0]); down = new TextureRegionDrawable(tr[0][1]); imageButton = new ImageButton(up,down); imageButton.setPosition(100, 100); stage.addActor(imageButton); Gdx.input.setInputProcessor(stage);//让舞台接收输入
-------------------------------------------------------------------------------------
这里的 TextureRegion[][] tmp = TextureRegion.split(tex, 120, 120);
参考 第5篇 - 动画绘制
-------------------------------------------------------------------------------------
完整代码:
package com.me.mygdxgame; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL10; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture.TextureFilter; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.ImageButton; import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle; import com.badlogic.gdx.scenes.scene2d.ui.Image; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; public class MyGdxGame implements ApplicationListener { Stage stage; TextureRegionDrawable up; TextureRegionDrawable down; TextureRegion buttonUp; TextureRegion buttonDown; Texture tex; ImageButton button; @Override public void create() { tex = new Texture(Gdx.files.internal("data/control.png")); TextureRegion[][] tmp = TextureRegion.split(tex, 120, 120); buttonUp = tmp[0][0]; buttonDown = tmp[0][1]; up = new TextureRegionDrawable(buttonUp); down = new TextureRegionDrawable(buttonDown); button = new ImageButton(up, down); button.setPosition(100, 100); stage = new Stage(480, 320, false); Gdx.input.setInputProcessor(stage); stage.addActor(button); } @Override public void dispose() { } @Override public void render() { Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); stage.act(); stage.draw(); } @Override public void resize(int width, int height) { } @Override public void pause() { } @Override public void resume() { } }
PS:大家可能看到模拟器上面的按钮看起来好像被拉伸了,这样按钮会不会很失败,
请大家放心,将来在游戏中我们用相机后,这个问题就会自然解决的,所以不必担心。
Comments NOTHING