我有一个游戏,您单击中间的图像,然后它的价值增加。打开应用程序后,我在主要活动开始之前(点击屏幕)发出了一个提示。但是,每次我退出该应用程序并再次单击该图标时,它都会闪过,进入主屏幕,然后再次开始游戏,将值设置回零。
我的Java for Splash:
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;
}
}
}
有人知道如何在最小化游戏时让我的游戏暂停和恢复吗?另外,是否有一种方法可以在销毁(正确关闭)应用程序并重新启动应用程序时保留变量的值?感谢您的帮助。
最佳答案
在第二个活动的onBackPress
内,应将分数存储在共享的首选项中。每次您进入onCreate
活动的Splash
时,检索分数值并检查其是否设置为0,然后显示启动画面,否则转到具有当前分数的主活动。