java – 在屏幕之间切换Libgdx

嘿大家我还在研究这个libgdx项目,我正在试图找出将屏幕更改为我的游戏画面的最佳方法现在,当点击一个按钮时,我需要它转换到游戏画面.我已经看到了一些扩展游戏类的实现,但我不确定这里最好的方法是什么.如果您看到一些可以改进的代码,请告诉我们.

这是主要的应用程序类:

public class ConnectFourApplication implements ApplicationListener {

    private Screen screen;

    public static void main(String[] args) {            
        new LwjglApplication(new ConnectFourApplication(),"PennyPop",1280,720,true);
    }

    @Override
    public void create() {
        screen = new MainScreen();
        screen.show();

    }

    @Override
    public void dispose() {
        screen.hide();
        screen.dispose();

    }

    /** Clears the screen with a white color */
    private void clearWhite() {
        Gdx.gl.glClearColor(1,1,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    }

    @Override
    public void pause() {
        screen.pause();
    }

    @Override
    public void render() {
        clearWhite();
        screen.render(Gdx.graphics.getDeltaTime());
    }

    @Override
    public void resize(int width,int height) {
        screen.resize(width,height);

    }

    @Override
    public void resume() {
        screen.resume();
    }

}


public class MainScreen implements Screen {

    private final Stage stage;
    private final SpriteBatch spriteBatch;

    //Parameter for drawing the buttons
    private final BitmapFont font;
    private final TextureAtlas buttons; 
    private final Button SFXButton;
    private final Button APIButton;
    private final Button GameButton;
    private final Skin images;

    //Parameter for Sound
    private final com.badlogic.gdx.audio.Sound SFXClick;

    //Parameter for the api call
    private final String WeatherUrl;
    private final HttpRequest request;
    private final City SanFrancisco;

    //The screen to load after the game button is hit
    private Screen gamescreen;


    public MainScreen() {

        //Set up our assets
        spriteBatch = new SpriteBatch();
        stage = new Stage(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(),false,spriteBatch);
        font = new BitmapFont(Gdx.files.internal("assets/font.fnt"),Gdx.files.internal("assets/font.png"),false);
        buttons = new TextureAtlas("assets/GameButtons.pack");
        images = new Skin(buttons);
        images.addRegions(buttons);
        SFXButton = new Button(images.getDrawable("sfxButton"));
        SFXButton.setPosition(295,310);
        APIButton = new Button(images.getDrawable("apiButton"));
        APIButton.setPosition(405,310);
        GameButton = new Button(images.getDrawable("gameButton"));
        GameButton.setPosition(515,310);
        SFXClick = Gdx.audio.newSound(Gdx.files.internal("assets/button_click.wav"));

        //Add our actors to the stage
        stage.addActor(SFXButton);
        stage.addActor(APIButton);
        stage.addActor(GameButton);

        //Set up our Url request to be used when clicking the button
        WeatherUrl = "http://api.openweathermap.org/data/2.5/weather?q=San%20Francisco,US";
        request = new HttpRequest(HttpMethods.GET);
        request.setUrl(WeatherUrl);
        SanFrancisco = new City("Unavailable","Unavailable","0","0"); //init san fran to be displayed before they have clicked the button


        //initialize the game screen that we will switch to when the button is pressed
        gamescreen = new GameScreen();

    }

    @Override
    public void dispose() {
        spriteBatch.dispose();
        stage.dispose();
    }

    @Override
    public void render(float delta) {
        stage.act(delta);
        stage.draw();

        //Begin sprite batch
        spriteBatch.begin();


        //Set our on click listeners for our buttons
        if (SFXButton.isPressed())
            SFXClick.play();

        if(APIButton.isPressed())
        {
            CallApi();
        }

        if(GameButton.isPressed())
            LoadGame();


        //Set font color and render the screen
        font.setColor(Color.RED);
        font.draw(spriteBatch,455 - font.getBounds("PennpyPop").width/2,460 + font.getBounds("PennyPop").height/2);

        font.setColor(Color.BLACK);
        font.draw(spriteBatch,"Current Weather",900 - font.getBounds("PennpyPop").width/2,460 + font.getBounds("PennyPop").height/2);

        font.setColor(Color.LIGHT_GRAY);
        font.draw(spriteBatch,SanFrancisco.Name,940 - font.getBounds("PennpyPop").width/2,420 + font.getBounds("PennyPop").height/2);


        font.setColor(Color.RED);
        font.draw(spriteBatch,SanFrancisco.CurrentCondition,950 - font.getBounds("PennpyPop").width/2,300 + font.getBounds("PennyPop").height/2);


        font.draw(spriteBatch,SanFrancisco.Temperature + " Degrees,",920 - font.getBounds("PennpyPop").width/2,270 + font.getBounds("PennyPop").height/2);

        font.draw(spriteBatch,SanFrancisco.WindSpeed,1200 - font.getBounds("PennpyPop").width/2,270 + font.getBounds("PennyPop").height/2);



        //End or sprite batch
        spriteBatch.end();


    }

    //Handles calling our API
    public void CallApi(){

        //Sends our stored HTTPRequest object
         Gdx.net.sendHttpRequest(request,new HttpResponseListener() {
            @Override
            public void handleHttpResponse(HttpResponse httpResponse) {

                //Uses our private response reader object to give us a the JSON from the api call
                JSONObject json =  HttpResponseReader(httpResponse);

                //Gets the name of the city
                SanFrancisco.Name = (String) json.get("name");      

                //Parsed through our returned JSON for the weather key
                JSONArray WeatherArray = (JSONArray) json.get("weather");
                //Gets the actual weather dictionary 
                JSONObject Weather = (JSONObject) WeatherArray.get(0);
                //Finally get the value with the key of description and assign it 
                //To the San Fran current conditions field
                SanFrancisco.CurrentCondition = (String) Weather.get("description");



                //Gets the actual main dictionary 
                JSONObject main = (JSONObject) json.get("main");            
                //Finally get the values based on the keys
                SanFrancisco.Temperature = (String) Double.toString((double) main.get("temp"));

                //Finally get the wind speed
                JSONObject wind = (JSONObject) json.get("wind");    
                SanFrancisco.WindSpeed = (String) Double.toString((double) wind.get("speed"));

            }

            @Override
            public void failed(Throwable t) {
                Gdx.app.log("Failed ",t.getMessage());

            }
         });
    }

    //When the button game button is clicked should load the connect four game
    public void LoadGame(){
        hide();
        gamescreen.show();
    }



    //Converts our HttpResponse into a JSON OBject
    private static JSONObject HttpResponseReader(HttpResponse httpResponse){

        BufferedReader read = new BufferedReader(new InputStreamReader(httpResponse.getResultAsStream()));  
        StringBuffer result = new StringBuffer();
        String line = "";

        try {
          while ((line = read.readLine()) != null) {
                  result.append(line);
              }

              JSONObject json;
            try {
                json = (JSONObject)new JSONParser().parse(result.toString());
                return json;
            } catch (ParseException e) {
                e.printStackTrace();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }

    @Override
    public void resize(int width,int height) {
        stage.setViewport(width,height,false);
    }

    @Override
    public void hide() {
        Gdx.input.setInputProcessor(null);
    }

    @Override
    public void show() {
        Gdx.input.setInputProcessor(stage);
        render(0);
    }

    @Override
    public void pause() {
        // Irrelevant on desktop,ignore this
    }

    @Override
    public void resume() {
        // Irrelevant on desktop,ignore this
    }
}

解决方法

这就是我总是实现屏幕切换的方式:

首先,主类需要扩展Game(来自com.badlogic.gdx.Game),你需要有一个类型为Game的新字段:

public class ConnectFourApplication extends Game{
     private Game game;

现在在构造函数中初始化游戏:

public ConnectFourApplication(){
     game = this; // Since this class extends Game

现在,要将屏幕设置为MainScreen,您需要做的就是使用setScreen(new MainScreen(game));方法(传递游戏,所以我们可以从MainScreen类设置屏幕)
您现在需要一个MainScreen类的新构造函数和一个新字段:

private Game game;
public MainScreen(Game game){
     this.game = game;

现在你可以使用game.setScreen(新屏幕(游戏));将屏幕设置为另一个实现Screen的类.

但是现在,在main类中,在render()方法中你必须使用super.render();使用其他屏幕渲染的一切!

public void render() {
    clearWhite();
    super.render();
}

PS:确保您作为屏幕制作的课程实际上实现了Screen.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


摘要:本文由葡萄城技术团队于博客园发布。转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具、解决方案和服务,赋能开发者。 前言 在Java编程中,循环结构是程序员常用的控制流程,而for循环和foreach循环是其中比较常见的两种形式。关于它们哪一个更快的讨论一直存在。本文旨在探究Java
摘要:本文由葡萄城技术团队于博客园原创并首发。转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具、解决方案和服务,赋能开发者。 前言 把数据导出至 Excel 是很常见的需求,而数据的持久化,往往又放在数据库中。因此把数据库中的数据导出到 Excel中,成了非常普遍的一个需求。 以关系型数
前言 在Excel中创建的大多数商业报告不是单页的文档,而是包含了多个上下文相关的信息,这些信息被存储在多个工作表中。例如我们的一些地区销售报告、按部门分类的员工记录、每家店铺的库存清单等。 然而,随着Excel文件中工作表数量的增加,要在单一文档内导航和管理数据会变得十分具有挑战性。此外,因为这些
本文由葡萄城技术团队发布。转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具、解决方案和服务,赋能开发者。 前言 本文小编将详细解析Spring Boot框架,并通过代码举例说明每个层的作用。我们将深入探讨Spring Boot的整体架构,包括展示层、业务逻辑层和数据访问层。通过这些例子,
摘要:本文由葡萄城技术团队原创并首发。转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具、解决方案和服务,赋能开发者。 前言 在Java中,开发者可以使用一些开源的库(如Apache POI)来添加、修改和处理Excel中的数据:包括数字、文本、日期、列表等。每种数据验证类型都具有不同的参
摘要:本文由葡萄城技术团队于博客园原创并首发。转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具、解决方案和服务,赋能开发者。 前言 Excel文件保护是常用的一种功能,文件保护主要有三种: 添加密码,如果没有密码不允许打开文件。 添加密码,如果没有密码,不能修改文件,但可以打开,只读以及
摘要:本文由葡萄城技术团队原创并首发。转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具、解决方案和服务,赋能开发者。 前言 数据透视分析是一种强大的工具,可以帮助我们从大量数据中提取有用信息并进行深入分析。而在Java开发中,可以借助PivotTable,通过数据透视分析揭示数据中的隐藏
摘要:本文由葡萄城技术团队原创并首发。转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具、解决方案和服务,赋能开发者。 前言 GrapeCity Documents for Excel(以下简称GcExcel)是葡萄城公司的一款支持批量创建、编辑、打印、导入/导出Excel文件的服务端表格
系列文章: 《还在担心报表不好做?不用怕,试试这个方法》(一) 《还在担心报表不好做?不用怕,试试这个方法》(二) 《还在担心报表不好做?不用怕,试试这个方法》(三) 概要 在上一篇文章《还在担心报表不好做?不用怕,试试这个方法》(三)中,小编为大家分享了数据间的主从关系及单元格布局。主要讲解数据之
前言 在现代软件开发中,重复性的增删改查逻辑代码的编写往往非常耗时且容易出错。为了提高开发效率,减少手动维护的成本,代码生成器就成为了一个非常重要的工具,本文小编就将为大家介绍一下如何利用一个开源项目快速生成数据接口。 实现方式 环境准备 技术栈:Java,Spring-Boot,MyBatisPl
引言 在当今快速发展的数字化时代,企业对业务应用的需求日益复杂且多元。低代码开发平台作为一个创新的解决方案,以直观易用的设计理念,打破了传统的编程壁垒,让非技术人员也能轻松构建功能完备的Web应用程序,无需深入编码。这一特性极大地简化了应用开发流程,加速了业务需求转化为实际应用的速度,为企业带来了前
系列文章: 《还在担心报表不好做?不用怕,试试这个方法》(一) 《还在担心报表不好做?不用怕,试试这个方法》(二) 概要 在上一篇文章《还在担心报表不好做?不用怕,试试这个方法》(二)中,小编介绍了模板语言中的的一些基本概念和用法,今天小编将继续为大家介绍如何不同字段间的父子关系,是如何在模板语言中
前言 随着软件开发的快速发展和需求的不断增长,开发人员面临着更多的压力和挑战。传统的开发方法需要花费大量的时间和精力,而低代码开发平台的出现为开发人员提供了一种更加高效、快速的开发方式。今天小编就以构建命令插件为例,展示如何使用Java语言高效构建自定义插件。 环境准备 活字格插件构建工具-Java
摘要:本文由葡萄城技术团队原创并首发。转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具、解决方案和服务,赋能开发者。 前言 在实际开发过程中,经常会有这样的需求:将Excel表格或特定区域转换为图片,以便在其他软件中使用。而在Java开发中,借助于报表插件可以轻松地将工作表、任意指定区域
本文由葡萄城技术团队原创并首发。转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具、解决方案和服务,赋能开发者。 什么是迭代计算 迭代计算其实是在 Excel 中,一种公式的循环引用,对于了解编程概念的同学,很容易会想到另一个词“递归”。 简单的说,就是一段程序调用自己,反复执行的逻辑。递
摘要:本文由葡萄城技术团队于博客园原创并首发。转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具、解决方案和服务,赋能开发者。 前言 在Excel中设计表单时,我们经常需要对收集的信息进行统计分析。例如,学校给老师统计课时,医院给医护人员统计班次等。传统的手工方式需要逐个对比数据,然后将计
前言 在我们使用Excel时,经常会遇到一个问题,就是导入Excel时公式显示为【#Ref!】的情况。这通常是因为公式中引用的单元格已被删除或对应的工作表被删除,导致原公式无法识别对应的参数而显示为【#Ref!】。 比如在一张Excel表中,sheet1 中 A1 单元格的公式为‘=Sheet2!B
前言 Java是一种广泛使用的编程语言,它在企业级应用开发中发挥着重要作用。而在实际的开发过程中,我们常常需要处理各种数据格式转换的需求。今天小编为大家介绍下如何使用葡萄城公司的的Java API 组件GrapeCity Documents for Excel(以下简称为GcExcel)将Excel
摘要:本文由葡萄城技术团队原创并首发。转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具、解决方案和服务,赋能开发者。 前言 在数据处理或者数据分析的场景中,需要对已有的数据进行排序,在Excel中可以通过排序功能进行整理数据。而在Java中,则可以借助Excel表格插件对数据进行批量排序
前言 GrapeCity Documents for Excel (以下简称GcExcel) 是葡萄城公司的一款服务端表格组件,它提供了一组全面的 API 以编程方式生成 Excel (XLSX) 电子表格文档的功能,支持为多个平台创建、操作、转换和共享与 Microsoft Excel 兼容的电子