libGDX游戏开发之弹窗(五)

23 篇文章 4 订阅
23 篇文章 8 订阅

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所需的文件如下:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌康ACG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值