【问题标题】:I need help making my game pause and resume我需要帮助让我的游戏暂停和恢复
【发布时间】:2014-04-20 22:18:06
【问题描述】:

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

我的飞溅 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;
    }


}

}

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

【问题讨论】:

    标签: java android onresume onpause


    【解决方案1】:

    在第二个活动的onBackPress 中,您应该将分数存储在共享偏好中。每次您进入onCreateSplash 活动时,检索分数值并检查它是否设置为0,然后显示启动屏幕,否则以当前分数转到主要活动。

    【讨论】:

    • 记得在游戏结束时将分数设置回 0。
    • @mbs 确切地说,使用 sharedpreferences 来维护游戏状态。
    猜你喜欢
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 2014-05-16
    相关资源
    最近更新 更多