Java 用户退出后如何重新启动游戏(Libgdx)

Java 用户退出后如何重新启动游戏(Libgdx),java,libgdx,Java,Libgdx,在我的libgdx游戏中,它按照我希望的方式运行,但当我退出游戏时,它会从我之前的位置开始,我希望它重新启动。代码如下 package com.me.fixGame; import java.util.Random; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input.Keys; import com.badlogic.g

在我的libgdx游戏中,它按照我希望的方式运行,但当我退出游戏时,它会从我之前的位置开始,我希望它重新启动。代码如下

package com.me.fixGame;

import java.util.Random;









import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;


import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
//import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.utils.Scaling;
import com.sun.jmx.snmp.tasks.Task;


public class fixGame implements ApplicationListener {
    SpriteBatch batch;
    SpriteBatch spriteBatch;
    Texture trash; 
    Texture paper;
    SpriteBatch spritebatch;
    Vector2 position;
    Vector2 pas;
    boolean collide;
    boolean countMe=false;
    Vector2 size;
    Vector2 size2;
    Vector2 pos;
    Rectangle bounds;
    float posSpeed=30;
    Rectangle bounds2;
    float delay = 1; // seconds
boolean counted= false;
    int score = 3;
    //Texture Gogreen;
    String myScore;
    Texture background; 
    CharSequence str = "Lives left: 3"; // = myScore;
    CharSequence line = "Score: 0"; // = myScore;
    String myLife;
    int life=0;
    BitmapFont font;
    float x;
    float y;
    boolean collision = false;
    @Override
    public void create() {  

     //Gogreen = new Texture(Gdx.files.internal("data/gogreenNow.jpg"));
     background = new Texture(Gdx.files.internal("data/trash.png"));

     x= background.getWidth();
     y=background.getHeight();
        //float delaySeconds = 1;
        spriteBatch = new SpriteBatch();
    trash = new Texture(Gdx.files.internal("data/trash.png"));
    paper = new Texture(Gdx.files.internal("data/paper1.jpg"));
    position = new Vector2(100, 50);
    pos = new Vector2(54, 14);
    batch = new SpriteBatch();
    BitmapFont font = new BitmapFont();

    size2 = new Vector2(trash.getWidth() ,trash.getHeight() );
    //size2.y = trash.getHeight();
    //size2.x = trash.getWidth();
    size = new Vector2(paper.getWidth() ,paper.getHeight());

    bounds= new Rectangle(pos.x, pos.y, size.x, size.y);
    bounds2= new Rectangle(position.x, position.y, size2.x, size2.y);

    }

    @Override
    public void dispose() {

    }
    public void update(){
        bounds.set(pos.x, pos.y, size.x, size.y);
        bounds2.set(position.x, position.y, size2.x, size2.y);
        float pos1=Gdx.input.getAccelerometerX();
        //if(pos1<0)
        //  pos1=(-1)*pos1;
        position.x = position.x - 5*pos1;
    }




    @Override
    public void render() {
        if(bounds.overlaps(bounds2)){
            collision=true; 
            counted=true;
        }else{
            collision=false;
        }

        if(collision==true){

        } 
        if(pos.y<640){
            counted=false;
        } else if(pos.y > 640 && collision==false && counted==false){
            counted=true;
            score= score-1;
            myScore = "Lives left: " + score;
            str = myScore;
        }



        if(bounds.overlaps(bounds2)){

            countMe=true;
            life= life+50;
            myLife = "Score: " + life;
            line = myLife;
        }


        if(position.x<0){
            position.x= position.x+11;
        }
        if(position.x>425){
            position.x= position.x-11;
        }


          update();
            Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        pos.y=pos.y-posSpeed;
        //posSpeed = posSpeed+(2/3);
        if(pos.y<0){
            pos.y = 700;
            Random randomGenerator = new Random();
              pos.x = randomGenerator.nextInt(500);
        }
        BitmapFont font = new BitmapFont();


        batch.begin();

         batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
         if (!collision) {
                batch.draw(paper, pos.x, pos.y);

                }

        //batch.draw(paper, pos.x, pos.y);
        batch.draw(trash, position.x, position.y);
        font.setScale(3);

        font.setColor(0.0f, 0.0f, 1.0f,1.0f);
        font.draw(batch, str, 300,900);
        font.draw(batch, line, 300, 950);
                batch.end();
                font.dispose();     
    }

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

    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }
}
package com.me.fixGame;
导入java.util.Random;
导入com.badlogic.gdx.ApplicationListener;
导入com.badlogic.gdx.gdx;
导入com.badlogic.gdx.Input.Keys;
导入com.badlogic.gdx.graphics.GL10;
导入com.badlogic.gdx.graphics.Texture;
导入com.badlogic.gdx.graphics.g2d.BitmapFont;
导入com.badlogic.gdx.graphics.g2d.SpriteBatch;
导入com.badlogic.gdx.graphics.g2d.TextureRegion;
导入com.badlogic.gdx.math.Rectangle;
导入com.badlogic.gdx.math.Vector2;
//导入com.badlogic.gdx.scenes.scene2d.Stage;
导入com.badlogic.gdx.scenes.scene2d.ui.Table;
导入com.badlogic.gdx.utils.Scaling;
导入com.sun.jmx.snmp.tasks.Task;
公共类fixGame实现ApplicationListener{
喷雾批;
SpriteBatch SpriteBatch;
纹理垃圾;
纹理纸;
SpriteBatch SpriteBatch;
矢量2位置;
向量2-pas;
布尔碰撞;
布尔计数=假;
矢量2大小;
向量2大小2;
向量2位;
矩形边界;
浮动速度=30;
矩形边界2;
浮点延迟=1;//秒
布尔计数=假;
智力得分=3分;
//纹理果绿;
字符串myScore;
纹理背景;
CharSequence str=“Lives left:3”;//=myScore;
CharSequence line=“分数:0”;//=myScore;
我的生活;
int-life=0;
位图字体;
浮动x;
浮动y;
布尔冲突=假;
@凌驾
public void create(){
//Gogreen=new纹理(Gdx.files.internal(“data/gogreenNow.jpg”);
背景=新纹理(Gdx.files.internal(“data/trash.png”);
x=background.getWidth();
y=background.getHeight();
//浮动延迟秒=1;
spriteBatch=新spriteBatch();
trash=新纹理(Gdx.files.internal(“data/trash.png”);
paper=新纹理(Gdx.files.internal(“data/paper1.jpg”);
位置=新矢量2(100,50);
pos=新矢量2(54,14);
批次=新的SpriteBatch();
BitmapFont字体=新的BitmapFont();
size2=新向量2(trash.getWidth(),trash.getHeight());
//size2.y=trash.getHeight();
//size2.x=trash.getWidth();
大小=新矢量2(paper.getWidth(),paper.getHeight());
边界=新矩形(位置x、位置y、大小x、大小y);
bounds2=新矩形(position.x,position.y,size2.x,size2.y);
}
@凌驾
公共空间处置(){
}
公共无效更新(){
边界设置(位置x、位置y、大小x、大小y);
bounds2.set(position.x,position.y,size2.x,size2.y);
float pos1=Gdx.input.getAccelerometerX();

//如果(pos1你说的是桌面还是安卓

假设您正在谈论Android,当用户退出游戏时,会调用pause()函数。当用户返回游戏时,会调用resume()函数

我敢打赌,如果你在退出和恢复游戏之间运行一些其他应用程序,你的游戏将“重置”。通常情况下,人们会在暂停()中保存游戏状态,然后在恢复()中加载它,但对于你的情况,听上去你每次都只想重置它


如果以上所有情况对您来说都是正确的,只需在resume()函数中重置游戏状态。

对于Android:如果用户按下“主页”按钮或来电,则调用游戏
pause()
方法。如果用户在通话后或一段时间后返回,通常
resume()
被调用。但是如果Android操作系统决定关闭你的应用程序,将调用
create()
,如果你不存储savegames,我相信它会重置游戏。
在您的情况下,用户没有退出游戏,而是通过按“主页”按钮“暂停”游戏。要重置游戏,您可以在
pause()
方法中调用
dispose()
,然后在dispose中只需关闭应用。在桌面
pause()据我所知,如果你切换窗口或最小化应用程序,就会调用
。如果你不想在这种情况下关闭应用程序,你必须控制它,如果它是桌面或android。

无意冒犯,但这不是编写复杂游戏的方法。保持适当的架构是的,我已经完成了,我不知道这一点……我是一名六年级学生.