Libgdx游戏引擎之Dialog组件
来源:互联网 发布:org.apache.cxf.jaxws 编辑:程序博客网 时间:2024/06/04 22:44
1、运行界面
2、示例代码
import com.badlogic.gdx.Gdx;import com.badlogic.gdx.graphics.Color;import com.badlogic.gdx.scenes.scene2d.Group;import com.badlogic.gdx.scenes.scene2d.InputEvent;import com.badlogic.gdx.scenes.scene2d.ui.Dialog;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.utils.ClickListener;import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;import com.swallowgames.supermario.game.Assets;import com.swallowgames.supermario.utils.Utils;/** * <b>类名称:</b>ExitDialog * <b>类描述:</b>退出游戏对话框 * <b>创建人:</b>wanglong * <b>修改人:</b>wanglong * <b>修改时间:</b>2014-12-14 上午3:54:18 * <b>修改备注:</b>类初始化 * @version 1.0.0<br/> */public class ExitDialog extends Dialog{public ExitDialog(String title, WindowStyle windowStyle) {super(title, windowStyle);TextureRegionDrawable textureRegionDrawable = (TextureRegionDrawable) windowStyle.background;float regionWidth = textureRegionDrawable.getRegion().getRegionWidth();float regionHeight = textureRegionDrawable.getRegion().getRegionHeight();setColor(1, 1, 1, 0);setSize(regionWidth, regionHeight);setOrigin(getWidth()/2, getHeight()/2);setPosition(Utils.xAxisCenter(getWidth()), Utils.yAxisCenter(getHeight()));//标题FreeTypeFontActor titleActor = new FreeTypeFontActor(title, 24);titleActor.setColor(Color.YELLOW);titleActor.setPosition((getWidth() - titleActor.getWidth())/2, getHeight() - 4);addActor(titleActor);getContentTable().debug();LabelStyle labelStyle = new LabelStyle(Assets.instance.bitmapTrueFont.getBitmapFont(32), Color.YELLOW);getContentTable().add(new Label("确定退出游戏吗?", labelStyle));getButtonTable().debug();//取消按钮Group cancel = new Group();final ImageButton cancelButton = new ImageButton(new TextureRegionDrawable(Assets.instance.assetUI.greenButton));cancel.addActor(cancelButton);labelStyle = new LabelStyle(Assets.instance.bitmapTrueFont.getBitmapFont(24), Color.BLACK);Label cancelLabel = new Label("取 消", labelStyle);cancelLabel.setPosition((cancelButton.getWidth() - cancelLabel.getWidth())/2, 2.5f + (cancelButton.getHeight() - cancelLabel.getHeight())/2);cancel.addActor(cancelLabel);cancel.setSize(cancelButton.getWidth(), cancelButton.getHeight());cancel.addListener(new ClickListener(){@Overridepublic void clicked(InputEvent event, float x, float y) {super.clicked(event, x, y);ExitDialog.this.hide();}});getButtonTable().add(cancel).padRight(40);//确定按钮Group confirm = new Group();final ImageButton confirmButton = new ImageButton(new TextureRegionDrawable(Assets.instance.assetUI.redButton));confirm.addActor(confirmButton);labelStyle = new LabelStyle(Assets.instance.bitmapTrueFont.getBitmapFont(24), Color.WHITE);Label confirmLabel = new Label("确 定", labelStyle);confirmLabel.setPosition((confirmButton.getWidth() - confirmLabel.getWidth())/2, 2.5f + (confirmButton.getHeight() - confirmLabel.getHeight())/2);confirm.addActor(confirmLabel);confirm.setSize(confirmButton.getWidth(), confirmButton.getHeight());confirm.addListener(new ClickListener(){@Overridepublic void clicked(InputEvent event, float x, float y) {super.clicked(event, x, y);Gdx.app.exit();}});getButtonTable().add(confirm).padLeft(40);//内容表格单元getCells().get(0).padTop(40);//按钮表格单元getCells().get(1).padBottom(40);}}
3、使用代码
显示
WindowStyle windowStyle = new WindowStyle(new BitmapFont(), Color.YELLOW, new TextureRegionDrawable(Assets.instance.assetUI.popdialog));ExitDialog exitWindow = new ExitDialog("系统提示", windowStyle);
<pre code_snippet_id="551993" snippet_file_name="blog_20141214_3_5353603" name="code" class="java">exitWindow.show(stage);
移除
exitWindow.hide();
4、代码说明
这个区域是getButtonTable组件
这个区域是getContentTable组件
Dialog集成Window本质是一个Table组件,通过以下代码可以调整ButtonTable和ContentTable的间距
//内容表格单元
getCells().get(0).padTop(40);
//按钮表格单元
getCells().get(1).padBottom(40);
其中titleActor是我自定义的一个标题,方便调整位置。
FreeTypeFontActor titleActor = new FreeTypeFontActor(title, 24);
titleActor.setColor(Color.YELLOW);
titleActor.setPosition((getWidth() - titleActor.getWidth())/2, getHeight() - 4);
addActor(titleActor);
Dialog显示和隐藏方法说明
show()
public Dialog show (Stage stage) {clearActions();removeCaptureListener(ignoreTouchDown);previousKeyboardFocus = null;Actor actor = stage.getKeyboardFocus();if (actor != null && !actor.isDescendantOf(this)) previousKeyboardFocus = actor;previousScrollFocus = null;actor = stage.getScrollFocus();if (actor != null && !actor.isDescendantOf(this)) previousScrollFocus = actor;pack();setPosition(Math.round((stage.getWidth() - getWidth()) / 2), Math.round((stage.getHeight() - getHeight()) / 2));stage.addActor(this);stage.setKeyboardFocus(this);stage.setScrollFocus(this);if (fadeDuration > 0) {getColor().a = 0;addAction(Actions.fadeIn(fadeDuration, Interpolation.fade));}return this;}该方法会把Satge上其他Actor的触摸事件,同时添加一个显示的动画
addAction(Actions.fadeIn(fadeDuration, Interpolation.fade));
避免我们自己去实现动画效果
hide()
/** Hides the dialog. Called automatically when a button is clicked. The default implementation fades out the dialog over * {@link #fadeDuration} seconds and then removes it from the stage. */public void hide () {Stage stage = getStage();if (stage != null) {if (previousKeyboardFocus != null && previousKeyboardFocus.getStage() == null) previousKeyboardFocus = null;Actor actor = stage.getKeyboardFocus();if (actor == null || actor.isDescendantOf(this)) stage.setKeyboardFocus(previousKeyboardFocus);if (previousScrollFocus != null && previousScrollFocus.getStage() == null) previousScrollFocus = null;actor = stage.getScrollFocus();if (actor == null || actor.isDescendantOf(this)) stage.setScrollFocus(previousScrollFocus);}if (fadeDuration > 0) {addCaptureListener(ignoreTouchDown);addAction(sequence(fadeOut(fadeDuration, Interpolation.fade), Actions.removeListener(ignoreTouchDown, true),Actions.removeActor()));} elseremove();}隐藏方法会恢复Stage上其他的触摸事件,同样也实现了一个隐藏的效果
addCaptureListener(ignoreTouchDown);
addAction(sequence(fadeOut(fadeDuration, Interpolation.fade), Actions.removeListener(ignoreTouchDown, true),
Actions.removeActor()));
该方法会将Dialog从Stage上移除
通过设置Dialog的fadeDuration值可以调整显示和消除的时间,默认是0.4s
- Libgdx游戏引擎之Dialog组件
- Libgdx游戏引擎之ScrollPane组件
- Android游戏引擎之LibGdx
- libgdx游戏引擎(三)之粒子系统
- libgdx游戏引擎(五)之自定义进度条
- Android 游戏引擎libgdx之Box2D
- libgdx游戏引擎教程
- libgdx游戏引擎教程
- libgdx游戏引擎(一)
- Android游戏引擎之LibGdx介绍及各模块概述
- Android游戏引擎之LibGdx介绍及各模块概述
- Android 游戏引擎libgdx之Box2D Hello Box2D
- libgdx游戏引擎(二)之ApplicationListener接口介绍
- libgdx游戏引擎(四)之舞台和演员
- libgdx游戏引擎(六)之资源异步加载
- LibGdx 游戏引擎 freetype 使用指南
- LibGDX游戏引擎-10-游戏地图(TiledMap)
- libgdx游戏引擎教程(三)libgdx粒子系统的使用
- java设计模式:Builder模式
- 转载猴子iap和Game Center
- mongodb入门
- WPF自适应窗体实现小结
- Android之使用SoundPool播放一小段音频,实现猜歌的功能
- Libgdx游戏引擎之Dialog组件
- 动态规划算法和贪心算法的比较
- 无法安装64位版本的office因为在您的pc上找到了以下32位程序:Microsoft Office Vision 2010
- ubuntu nginx 配置
- 观察者模式——委托与事件
- Web应用程序中的脚本—客户端和服务器端
- 数据结构中顺序表的基本操作(纯C语言)
- wpf 根据不同屏幕分辨率自动适应屏幕
- wikioi1439统计素数个数