栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

libGDX游戏开发之弹窗(五)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;


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;


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();
    }
}

按上下键显示关闭

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/301411.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号