libGDX游戏开发之弹窗(五)
libGDX游戏开发之弹窗(五)
libGDX系列,游戏开发有unity3D巴拉巴拉的,为啥还用java开发?因为我是Java程序员emm…国内用libgdx比较少,多数情况需要去官网和google找资料,相互学习的可以加我联系方式。
弹窗简单的代码如下:
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.ScreenUtils;
/**
* @author lingkang
* @date 2021/10/7 0:01
* @description
*/
public class MyWindow extends ApplicationAdapter {
// 定义一个对话框
private Dialog dialog;
private Stage stage;// 对话框需要一个舞台
@Override
public void create() {
stage=new Stage();
// http://github.com/libgdx/libgdx/tree/master/tests/gdx-tests-android/assets/data 这里是官方的开发专用
// 这皮肤也是比较尴尬的,框架没有内置样式,还得自己找。
Skin skin = new Skin(Gdx.files.internal("sk/uiskin.json"));
dialog = new Dialog("Info", skin); // 老规矩默认不支持中文
dialog.text("hello ?");
dialog.button("Yes", true); //sends "true" as the result
dialog.button("No", false); //sends "false" as the result
dialog.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
System.out.println(event);
dialog.hide();
}
});
// 必须给舞台添加输入,否则无法触发舞台上的按钮事件等
Gdx.input.setInputProcessor(stage);
}
@Override
public void render() {
// 清除相机内容
ScreenUtils.clear(225f, 225f, 225f, 0.4f);
// 按上键触发对话框
if (Gdx.input.isKeyPressed(Input.Keys.UP) || Gdx.input.isKeyPressed(Input.Keys.W)) {
dialog.show(stage);
}
// 绘制舞台
stage.draw();
// 舞台的行为,一定要添加,否则无法触发对话框的UI更新
stage.act();
}
}
效果如下

也可以在这里找到所有的皮肤字体等等:
https://github.com/rafaskb/awesome-libgdx#readme
那么我们也可以自定一个窗口
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
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.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.utils.ScreenUtils;
/**
* @author lingkang
* @date 2021/10/7 0:05
* @description
*/
public class MyWindow extends ApplicationAdapter {
// 自定义一个窗口
class MessageWindow extends Window {
public MessageWindow(String title, Skin skin) {
super(title, skin.get(WindowStyle.class));
setSkin(skin);
}
}
private Stage stage;// 对话框需要一个舞台
private MessageWindow messageWindow;
@Override
public void create() {
stage = new Stage();
// http://github.com/libgdx/libgdx/tree/master/tests/gdx-tests-android/assets/data 这里是官方的开发专用
// 这皮肤也是比较尴尬的,框架没有内置样式,还得自己找。
Skin skin = new Skin(Gdx.files.internal("sk/uiskin.json"));
messageWindow = new MessageWindow("custom window", skin);
messageWindow.add("custom window");
Button button = new Button(skin);
button.add(new Label("OK", skin));
button.setSize(40, 20);
messageWindow.add(button);
messageWindow.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
messageWindow.setWidth(100);
messageWindow.setHeight(100);
messageWindow.setSize(200, 200);
// 添加按钮事件
button.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
// 不可见
messageWindow.setVisible(false);
return super.touchDown(event, x, y, pointer, button);
}
});
// 不可见
messageWindow.setVisible(false);
stage.addActor(messageWindow);
Gdx.input.setInputProcessor(stage);
}
@Override
public void render() {
// 清除相机内容
ScreenUtils.clear(225f, 225f, 225f, 0.4f);
// 按上键触发对话框
if (Gdx.input.isKeyPressed(Input.Keys.UP) || Gdx.input.isKeyPressed(Input.Keys.W)) {
messageWindow.setVisible(false);
}
if (Gdx.input.isKeyPressed(Input.Keys.DOWN) || Gdx.input.isKeyPressed(Input.Keys.S)) {
messageWindow.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
messageWindow.setVisible(true);
}
// 绘制舞台
stage.draw();
// 舞台的行为,一定要添加,否则无法触发事件
stage.act();
}
}
按上下键显示关闭

其中sk所需的文件如下:

libGDX游戏开发之弹窗(五)的更多相关文章
- 《MFC游戏开发》笔记五 定时器和简单动画
本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9332377 作者:七十一雾央 新浪微博:http:// ...
- Libgdx游戏开发(2)——接水滴游戏实现
原文:Libgdx游戏开发(2)--接水滴游戏实现 - Stars-One的杂货小窝 本文使用Kotlin语言开发 通过本文的学习可以初步了解以下基础知识的使用: Basic file access ...
- [libGDX游戏开发教程]使用libGDX进行游戏开发(12)-Action动画
前文章节列表: 使用libGDX进行游戏开发(11)-高级编程技巧 使用libGDX进行游戏开发(10)-音乐音效不求人,程序员也可以DIY 使用libGDX进行游戏开发(9)-场景过渡 ...
- [libGDX游戏开发教程]使用libGDX进行游戏开发(1)-游戏设计
声明:<使用Libgdx进行游戏开发>是一个系列,文章的原文是<Learning Libgdx Game Development>,大家请周知.后续的文章连接在这里 使用Lib ...
- Unity3D ——强大的跨平台3D游戏开发工具(五)
第九章 图形用户界面类G.U.I 您在玩很多3D游戏的时候,不知是否注意到在游戏界面中,总有一些图形和文字信息是不随着3D视角的改变而改变的.这也是由于游戏本身的要求而决定的.比如说英雄的生命值,聊天 ...
- 精通libGDX游戏开发-RPG实战-开发游戏的基本前提
说起RPG,大概国人是不会陌生的. 这不得不从中国单机游戏市场说起,由于早期软件市场被盗版杀死,顺带的,单机游戏软件作为软件市场的分支,也没赚什么钱,养不活公司纷纷倒闭,只到RPG游戏<仙剑奇侠 ...
- [libgdx游戏开发教程]使用Libgdx进行游戏开发(11)-高级编程技巧 Box2d和Shader
高级编程技巧只是相对的,其实主要是讲物理模拟和着色器程序的使用. 本章主要讲解利用Box2D并用它来实现萝卜雨,然后是使用单色着色器shader让画面呈现单色状态:http://files.cnblo ...
- [libgdx游戏开发教程]使用Libgdx进行游戏开发(10)-音乐和音效
本章音效文件都来自于公共许可: http://files.cnblogs.com/mignet/sounds.zip 在游戏中,播放背景音乐和音效是基本的功能. Libgdx提供了跨平台的声音播放功能 ...
- [libgdx游戏开发教程]使用Libgdx进行游戏开发(2)-游戏框架搭建
让我们抛开理论开始code吧. 入口类CanyonBunnyMain的代码: package com.packtpub.libgdx.canyonbunny; import com.badlogic. ...
- 精通libGDX游戏开发-RPG实战-欢迎来到RPG的世界
欢迎来到RPG的世界 本章我会快速的使用tiled这样的瓷砖地图工具,来带领大家创造所设想的世界. 创建并编辑瓷砖地图 瓷砖地图(tile-based map)是广泛应用于各种游戏类型的地图格式,li ...
随机推荐
- AT 下分记录
7.30 AGC063 \(+30=1620\) B 做法假 WA 了三次,为啥总是吃了罚时才能发现问题啊 心态还是需要解决的问题.过完 B 啥都想不出来又自闭了
- c语言代码练习3
//查看数字是否存在于数组中#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { int i = 0; int ...
- python - networker api
Getting Started,概述 NetWorker REST API is an interface that provides programmatic access to the Net ...
- 【XXE实战】——浅看两道CTF题
[XXE实战]--浅看两道CTF题 上一条帖子[XXE漏洞]原理及实践演示对XXE的一些原理进行了浅析,于是写了两道CTF题巩固一下,顺便也记录一下第一次写出来CTF.两道题都是在BUU上找的:[NC ...
- go使用snmp库查询mib数据
转载请注明出处: OID(Object Identifier)是一种用于标识和唯一命名管理信息库中的对象的标准方式.给定一个OID,可以确定特定的管理信息库对象,并对其进行操作. go语言使用snmp ...
- 数据结构与算法 | 链表(Linked List)
链表(Linked List)是一种线性数据结构,它由一系列节点(Node)组成,每个节点包含两部分:数据和指向下(上)一个节点的引用(或指针).链表中的节点按照线性顺序连接在一起(相邻节点不需要存储 ...
- Vue之for循环
Vue中for循环的用法总结如下: 1.基本用法 v-for <!DOCTYPE html> <html lang="en"> <head> & ...
- Linux常用命令(包含学习资源)
目录 (0)学习资源 (一)查看系统信息 (二)文件和目录 (三)文件搜索 (四)挂载一个文件系统 (五)磁盘空间 (六)用户和群组 (七)文件的权限 - 使用 "+" 设置权限, ...
- [转载]R2: 已解释和未解释的方差
估计值的方差与总体方差之间的差异就是回归方程对方差的解释率.试举一例,如图 1,身高与体重的回归线显示身高与体重之间呈正相关,Mr. Y身高76英寸体重220磅(图 1中插图.cdr的红点),他与体重 ...
- 测试技术:开源测试工具 jenkins、Sonar
http://www.cnblogs.com/itech/archive/2011/11/23/2260009.html Jenkins入门总结 基于 Jenkins 快速搭建持续集成环境 https ...