gpt4 book ai didi

java - 我需要帮助暂停和恢复游戏

转载 作者:行者123 更新时间:2023-12-01 13:07:09 25 4
gpt4 key购买 nike

我有一个游戏,您点击中心的图像,值就会增加。当应用程序打开时,我在主要 Activity 开始之前(点击屏幕)出现了一个启动画面。但是,每次我退出应用程序并再次单击该图标时,它都会经历启动画面,进入主屏幕,然后再次启动游戏,将值设置回零。

我的 Splash Java:

package com.bipbapapps.leagueclickerapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;


public class Splash extends Activity {

@Override
public void onCreate(Bundle splashBundle) {

// TODO Auto-generated method stub
super.onCreate(splashBundle);

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);



Thread logoTimer = new Thread(){
public void run(){
try {
sleep(2000);
Intent mainIntent = new Intent("com.bipbapapps.leagueclickerapp.CLICKER");
startActivity(mainIntent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

finally {
finish();
}
}
};
logoTimer.start();
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}

}

我的 MainClass Java,然后运行:

package com.bipbapapps.leagueclickerapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class MainClass extends Activity implements OnClickListener {

public float goldCount = 0.0f;
Button minionClick;
TextView textGoldCount;
String textTotal;

@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//Set fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.mainlayout);



//Linking the variables
minionClick = (Button) findViewById(R.id.minioncentreid);
textGoldCount = (TextView) findViewById(R.id.textviewtop);

//String which will display at the top of the app
textTotal = goldCount + " Gold";

//Setting TextView to the String
textGoldCount.setText(textTotal);

//Setting onClickListener
minionClick.setClickable(true);

minionClick.setOnClickListener(this);

}


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.minioncentreid:
goldCount += 1.0;
textTotal = goldCount + " Gold";
textGoldCount.setText(textTotal);
break;
}


}

}

有人知道如何让我的游戏在最小化时暂停和恢复吗?另外,有没有一种方法可以在应用程序被销毁(正确关闭)并重新启动时保留变量的值?感谢您的帮助。

最佳答案

在第二个 Activity 的 onBackPress 内,您应该将分数存储在共享首选项中。每次进入 Splash Activity 的 onCreate 时,检索分数值并检查它是否设置为 0,然后显示启动屏幕,否则转到具有当前分数的主 Activity 。

关于java - 我需要帮助暂停和恢复游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23183430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com