我需要帮助让我的游戏暂停和恢复

时间:2014-04-20 14:13:30

标签: java android onresume onpause

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

我的Java for the 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();
}

}

我的Java用于MainClass,然后运行:

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


}

}

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

1 个答案:

答案 0 :(得分:1)

在第二项活动的onBackPress内,您应该将分数存储在共享偏好设置中。每当您进入onCreate Splash活动时,检索得分值并检查它是否设置为0然后显示启动画面,否则转到当前得分的主要活动。