Dialog在游戏中也很常用,尤其在设置、退出、商店、暂停等画面。Dialog的使用也可以通过skin实现,也可以自定义。

下面是一个简单的实例:

 package com.fxb.newtest;

 import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Window.WindowStyle;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable; public class Lib007_Dialog implements ApplicationListener{ Stage stage;
Dialog dialog;
Drawable draw1;
Drawable draw2;
Pixmap pixmap;
Texture texture;
Skin skin;
BitmapFont font;
Dialog dialog2; @Override
public void create() {
// TODO Auto-generated method stub stage = new Stage();
skin = new Skin();
pixmap = new Pixmap( , , Format.RGBA8888 );
pixmap.setColor( Color.GRAY );
pixmap.fill();
//draw1 = new TextureRegionDrawable( );
//texture = new Texture( pixmap );
skin.add( "gray", new Texture(pixmap) ); pixmap.setColor( Color.LIGHT_GRAY );
pixmap.fill();
skin.add( "light_gray", new Texture(pixmap) ); font = new BitmapFont();
WindowStyle windowStyle = new WindowStyle( font, Color.GREEN, skin.getDrawable( "light_gray" ) );
dialog = new Dialog( "DialogTest", windowStyle );
dialog.setTitleAlignment( Align.center | Align.top );
dialog.setBounds( , , , ); LabelStyle labelStyle = new LabelStyle( font, Color.RED );
ButtonStyle buttonStyle = new ButtonStyle( skin.getDrawable("gray"), skin.getDrawable("light_gray"), null );
TextButtonStyle textbuttonStyle = new TextButtonStyle( skin.getDrawable("gray"), skin.getDrawable("light_gray"), null, font );
skin.add( "default", buttonStyle );
skin.add( "default", textbuttonStyle );
skin.add( "default", windowStyle );
skin.add( "default", labelStyle ); Button button1 = new Button( skin );
button1.setBounds( , , , );
TextButton textbutton1 = new TextButton( "Click", skin );
textbutton1.setBounds( -, , , );
dialog.addActor( button1 );
dialog.addActor( textbutton1 ); stage.addActor( dialog );
//dialog2.row().fill().expand(); dialog2 = new Dialog( "Dialog2", skin, "default" ){
@Override
protected void result(Object object) {
// TODO Auto-generated method stub
//System.out.println( "Chosen" + object );
if( object.toString().equals( "true" ) ){
System.out.println( "Yes" );
Gdx.app.exit();
}else{
System.out.println( "No" );
}
}
};
dialog2.text("\nDo you want to exit?").button("Yes", true).button("No",false).key(Keys.ENTER, true).key(Keys.ESCAPE,false);
dialog2.setTitleAlignment( Align.top );
dialog2.pack();
//dialog2.show( stage ); //TextButton textbutton2 = new TextButton( "Click", skin );
textbutton1.addListener( new ChangeListener()
{
@Override
public void changed(ChangeEvent event, Actor actor) {
// TODO Auto-generated method stub
dialog2.show( stage );
}
}); //stage.addActor( textbutton2 );
Gdx.input.setInputProcessor( stage );
} @Override
public void resize(int width, int height) {
// TODO Auto-generated method stub } @Override
public void render() {
// TODO Auto-generated method stub
Gdx.gl.glClearColor( , , , );
Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT ); stage.act();
stage.draw(); } @Override
public void pause() {
// TODO Auto-generated method stub
} @Override
public void resume() {
// TODO Auto-generated method stub
} @Override
public void dispose() {
// TODO Auto-generated method stub
skin.dispose();
stage.dispose();
texture.dispose();
} }

运行效果:

libgdx学习记录8——对话框Dialog的更多相关文章

  1. libgdx学习记录2——文字显示BitmapFont

    libgdx对中文支持不是太好,主要通过Hireo和ttf字库两种方式实现.本文简单介绍最基本的bitmapfont的用法. 代码如下: package com.fxb.newtest; import ...

  2. libgdx学习记录3——动画Animation

    libgdx动画采用Animation实现,即通过帧动画实现. 代码如下: package com.fxb.newtest; import com.badlogic.gdx.ApplicationAd ...

  3. libgdx学习记录26——Polygon多边形碰撞检测

    libgdx中Math封装了Polygon这个类,它是由多个定点进行描述实现的,在进行物体间的碰撞时,物体轮廓有时候是不规则的,这时候可以用一个多边形勾勒出其大概的轮廓,对其进行模拟. Polygon ...

  4. libgdx学习记录22——3d物体创建

    libgdx是一个强大的游戏框架,不仅支持2d部分,同时还支持3d部分. libgdx的3d部分投影主要通过PerspectiveCamera实现. 物体的显示过程: 1. 创建远景相机,角度一般设为 ...

  5. libgdx学习记录20——多线程MultiThread资源处理

    在libgdx中,一般的逻辑流程都在rende()函数中执行,这个函数是由opengl的渲染线程调用的,一般的图形显示和逻辑处理都在这个线程中. 一般情形下,在这个线程中处理就行了.但是当某些逻辑处理 ...

  6. libgdx学习记录19——图片动态打包PixmapPacker

    libgdx中,opengl 1.x要求图片长宽必须为2的整次幂,一般有如下解决方法 1. 将opengl 1.x改为opengl 2.0.(libgdx 1.0版本后不支持1.x,当然不存在这个问题 ...

  7. libgdx学习记录18——Box2d物理引擎

    libgdx封装了Box2D物理引擎,通过这个引擎能够模拟物理现实,使设计出的游戏更具有真实感. libgdx中,Box2d程序的大概过程: 1. 创建物理世界world,并设置重力加速度. 2. 创 ...

  8. libgdx学习记录17——照相机Camera

    照相机在libgdx中的地位举足轻重,贯穿于整个游戏开发过程的始终.一般我们都通过Stage封装而间接使用Camera,同时我们也可以单独使用Camera以完成背景的移动.元素的放大.旋转等操作. C ...

  9. libgdx学习记录16——资源加载器AssetManager

    AssetManager用于对游戏中的资源进行加载.当游戏中资源(图片.背景音乐等)较大时,加载时会需要较长时间,可能会阻塞渲染线程,使用AssetManager可以解决此类问题. 主要优点: 1. ...

随机推荐

  1. 【转】使用windeployqt.exe进行依赖查找打包

    原文:https://blog.csdn.net/u011822862/article/details/52166940 Qt 官方开发环境使用的动态链接库方式,在发布生成的可执行程序时,需要复制可执 ...

  2. LeetCode题解之Binary Tree Tilt

    1.题目描述 2.分析 利用递归实现. 3.代码 int findTilt(TreeNode* root) { if (root == NULL) ; ; nodesTilt(root,ans); r ...

  3. 文档撰写思路与排版(hadoop)

    这几天在写项目提交的几个报告,写完回想了一下,在写作框架确定与排版上浪费了不少时间,特此总结一下思路. 这个写完回家过年了....emmmm 1. 定好大标题框架,使用自动添加序号,先不着急修改样式 ...

  4. 初识java内存区域

    目录: 1.运行时数据区域 2.对象的创建 3.对象的内存布局 4.对象的访问定位 一.运行时数据区域 基本的java虚拟机运行时数据区如下图: 下面我们就来逐个认识这几个运行时的数据区域 1.程序计 ...

  5. Yearning v1.3.0 发布,Web 端 SQL 审核平台

    企业级MYSQL web端 SQL审核平台. Website 官网 www.yearning.io Feature 功能 数据库字典自动生成 SQL查询 查询工单 导出 自动补全,智能提示 查询语句审 ...

  6. guider – 全系统Linux性能分析器

    Guider是一个免费且开源的,功能强大的全系统性能分析工具,主要以Python for Linux 操作系统编写. 它旨在衡量系统资源使用量并跟踪系统行为,从而使其可以有效分析系统性能问题或进行性能 ...

  7. MySQL: Building the best INDEX for a given SELECT

    Table of Contents The ProblemAlgorithmDigressionFirst, some examplesAlgorithm, Step 1 (WHERE "c ...

  8. 转:基于InstallShield2013LimitedEdition的安装包制作

    InstallShield Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序)   InstallShield Limited Edit ...

  9. CentOS7 中安装 MySQL

    0. 说明 参考 centos7.2安装MySQL CentOS 7 下 Yum 安装 MySQL 5.7 两种方式安装 MySQL 安装 MySQL(yum) & 安装 MySQL(yum) ...

  10. 乘风破浪:LeetCode真题_024_Swap Nodes in Pairs

    乘风破浪:LeetCode真题_024_Swap Nodes in Pairs 一.前言 这次还是链表的操作,不过我们需要交换链表奇数和偶数位置上的节点,因此要怎么做呢? 二.Swap Nodes i ...