libgdx学习记录8——对话框Dialog
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的更多相关文章
- libgdx学习记录2——文字显示BitmapFont
libgdx对中文支持不是太好,主要通过Hireo和ttf字库两种方式实现.本文简单介绍最基本的bitmapfont的用法. 代码如下: package com.fxb.newtest; import ...
- libgdx学习记录3——动画Animation
libgdx动画采用Animation实现,即通过帧动画实现. 代码如下: package com.fxb.newtest; import com.badlogic.gdx.ApplicationAd ...
- libgdx学习记录26——Polygon多边形碰撞检测
libgdx中Math封装了Polygon这个类,它是由多个定点进行描述实现的,在进行物体间的碰撞时,物体轮廓有时候是不规则的,这时候可以用一个多边形勾勒出其大概的轮廓,对其进行模拟. Polygon ...
- libgdx学习记录22——3d物体创建
libgdx是一个强大的游戏框架,不仅支持2d部分,同时还支持3d部分. libgdx的3d部分投影主要通过PerspectiveCamera实现. 物体的显示过程: 1. 创建远景相机,角度一般设为 ...
- libgdx学习记录20——多线程MultiThread资源处理
在libgdx中,一般的逻辑流程都在rende()函数中执行,这个函数是由opengl的渲染线程调用的,一般的图形显示和逻辑处理都在这个线程中. 一般情形下,在这个线程中处理就行了.但是当某些逻辑处理 ...
- libgdx学习记录19——图片动态打包PixmapPacker
libgdx中,opengl 1.x要求图片长宽必须为2的整次幂,一般有如下解决方法 1. 将opengl 1.x改为opengl 2.0.(libgdx 1.0版本后不支持1.x,当然不存在这个问题 ...
- libgdx学习记录18——Box2d物理引擎
libgdx封装了Box2D物理引擎,通过这个引擎能够模拟物理现实,使设计出的游戏更具有真实感. libgdx中,Box2d程序的大概过程: 1. 创建物理世界world,并设置重力加速度. 2. 创 ...
- libgdx学习记录17——照相机Camera
照相机在libgdx中的地位举足轻重,贯穿于整个游戏开发过程的始终.一般我们都通过Stage封装而间接使用Camera,同时我们也可以单独使用Camera以完成背景的移动.元素的放大.旋转等操作. C ...
- libgdx学习记录16——资源加载器AssetManager
AssetManager用于对游戏中的资源进行加载.当游戏中资源(图片.背景音乐等)较大时,加载时会需要较长时间,可能会阻塞渲染线程,使用AssetManager可以解决此类问题. 主要优点: 1. ...
随机推荐
- 机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集
机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集 关键字:FPgrowth.频繁项集.条件FP树.非监督学习作者:米 ...
- 查询es curl命令记录
curl -H "Content-Type: application/json" -XGET http://10.65.0.33:9200/online/senseLog/_se ...
- LeetCode题解之Binary Tree Tilt
1.题目描述 2.分析 利用递归实现. 3.代码 int findTilt(TreeNode* root) { if (root == NULL) ; ; nodesTilt(root,ans); r ...
- 转:.NET 面试题汇总(二)
目录 本次给大家介绍的是我收集以及自己个人保存一些.NET面试题第二篇 简介 1.接口 2.您在什么情况下会用到虚方法或抽象类,接口? 3.重载(Overload )和覆写(Override)的区别 ...
- Unity By Reflection Update Scripts
App热更新需求 我正在使用Unity 3D开发一个Android的应用,它会下载AssetBundles并加载它们的内容,但由于AssetBundles不能包含脚本,我将使用预编译的C#脚本,并使用 ...
- [Spark SQL_3] Spark SQL 高级操作
0. 说明 DataSet 介绍 && Spark SQL 访问 JSON 文件 && Spark SQL 访问 Parquet 文件 && Spark ...
- vue2.0学习笔记之路由(二)路由嵌套+动画
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 详解--从地址栏输入url到页面展现中间都发生了什么?
这是一个综合性很强的问题,个人理解包含以下七个基本点: 1.在浏览器地址栏输入url并按下回车. 2.浏览器检查当前url是否存在缓存和缓存是否过期. 3.域名解析(DNS解析url对应的ip). 4 ...
- swiper.js 多图片页面的懒加载lazyLoading
swiper.js官网:http://www.swiper.com.cn/api/Images/2015/0308/213.html 设为true开启图片延迟加载,使preloadImages无效.需 ...
- Volley源码分析(四)NetWork与ResponseDelivery工作原理
这篇文章主要分析网络请求和结果交付的过程. NetWork工作原理 之前已经说到通过mNetWork.performRequest()方法来得到NetResponse,看一下该方法具体的执行流程,pe ...