我正在创建一个有多个级别的游戏。 我在resume函数中使用整数共享首选项,然后我将舞台放入其中。 要将此值放在共享首选项中,请使用以下代码。
private void resetGame() {
position = 0;
SharedPreferences shared = getSharedPreferences("Prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putInt("stage", stage);
editor.commit();
String dashes = "";
switch(stage){
case 0:
{
db.open();
String st=db.newword(1);
currentWord = st;
byte[] p=db.getpic(1);
Bitmap bm=BitmapFactory.decodeByteArray(p, 0, p.length);
img.setImageBitmap(bm);
break;
}
并且在每个级别获胜后
Toast.makeText(Main.this, "You win!", Toast.LENGTH_SHORT).show();
stage++;
resetGame();
return;
但它起作用了......我希望你能帮助我,谢谢很多:)
它的总代码我尝试了所有的建议,但没有成功 当我退出游戏时,关卡从开始开始
public class Main extends Activity {
private String currentWord;
private TextView txtDashes;
private ImageView img;
private Button button18;
private database db;
private int position;
private ArrayList<Button> invisibleButtons = new ArrayList<Button>();
private int nCorrect;
private int stage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtDashes = (TextView) findViewById(R.id.txtDashes);
Button btnNew = (Button) findViewById(R.id.btnNew);
img=(ImageView)findViewById(R.id.img);
button18 =(Button) findViewById(R.id.Button18);
db=new database(this);
db.useable();
stage= 0;
btnNew.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
resetGame();
}
});
position=0;
resetGame();
}
private void resetGame() {
position = 0;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String dashes = "";
switch(stage){
case 0:
{
stage = prefs.getInt("stage", 0);
db.open();
String st=db.newword(1);
currentWord = st;
byte[] p=db.getpic(1);
Bitmap bm=BitmapFactory.decodeByteArray(p, 0, p.length);
img.setImageBitmap(bm);
break;
}
case 1:{
stage = prefs.getInt("stage", 1);
db.open();
String st=db.newword(2);
currentWord = st;
byte[] p=db.getpic(2);
Bitmap bm=BitmapFactory.decodeByteArray(p, 0, p.length);
img.setImageBitmap(bm);
break;
}
case 2:{
stage = prefs.getInt("stage", 2);
db.open();
String st=db.newword(3);
currentWord = st;
byte[] p=db.getpic(3);
Bitmap bm=BitmapFactory.decodeByteArray(p, 0, p.length);
img.setImageBitmap(bm);
break;
}
case 3:{
stage = prefs.getInt("stage", 3);
db.open();
String st=db.newword(4);
currentWord = st;
byte[] p=db.getpic(4);
Bitmap bm=BitmapFactory.decodeByteArray(p, 0, p.length);
img.setImageBitmap(bm);
img.setImageBitmap(bm);
db.close();
break;
}
}
for (int i = 0; i < currentWord.length(); i++) {
dashes = dashes + "-";
}
txtDashes.setText(dashes);
for (Button btn: invisibleButtons) {
btn.setVisibility(View.VISIBLE);
}
invisibleButtons.clear();
nCorrect = 0;
}
public void letterOnClickListener(View view) {
Button btn = (Button) view;
btn.setVisibility(View.INVISIBLE);
invisibleButtons.add(btn);
char letter = btn.getText().toString().toCharArray()[0];
char[] dashesArray = txtDashes.getText().toString().toCharArray();
char[] charArray = currentWord.toCharArray();
if (charArray[position] == letter) {
nCorrect++;
}
dashesArray[position] = letter;
position ++;
txtDashes.setText(new String(dashesArray));
if (nCorrect == currentWord.length()) {
Toast.makeText(Main.this, "You win!", Toast.LENGTH_SHORT).show();
stage++;
resetGame();
return;
}
else {
if (position >= currentWord.length()) {
Toast.makeText(Main.this, "You lose!", Toast.LENGTH_SHORT).show();
resetGame();
return;
}
}
}
}
答案 0 :(得分:0)
通过resetGame方法传递上下文
Toast.makeText(Main.this, "You win!", Toast.LENGTH_SHORT).show();
stage++;
resetGame(Main.this);
return;
仅使用上下文使用SharedPrefs:
private void resetGame(Context context){
SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = shared.edit();
editor.apply();
}
答案 1 :(得分:0)
在代码中的某处,您必须从共享首选项中检索舞台。
private void resetGame() {
position = 0;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
stage = prefs.getInt("stage", 0);
String dashes = "";
switch(stage){
case 0:
{
db.open();
String st=db.newword(1);
currentWord = st;
byte[] p=db.getpic(1);
Bitmap bm=BitmapFactory.decodeByteArray(p, 0, p.length);
img.setImageBitmap(bm);
break;
}
}
@Override
protected void onPause() {
SharedPreferences shared = getSharedPreferences("Prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putInt("stage", stage);
editor.apply();
}
答案 2 :(得分:0)
你错了,stage = prefs.getInt("stage", 0);
应该在switch语句不在里面。
private void resetGame() {
position = 0;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String dashes = "";
stage = prefs.getInt("stage", 0); // <--- move this here
switch(stage){
case 0:
{
//stage = prefs.getInt("stage", 0);
db.open();
String st=db.newword(1);
currentWord = st;
byte[] p=db.getpic(1);
Bitmap bm=BitmapFactory.decodeByteArray(p, 0, p.length);
img.setImageBitmap(bm);
break;
}
case 1:{
//stage = prefs.getInt("stage", 1);
db.open();
String st=db.newword(2);
currentWord = st;
byte[] p=db.getpic(2);
Bitmap bm=BitmapFactory.decodeByteArray(p, 0, p.length);
img.setImageBitmap(bm);
break;
}
case 2:{
//stage = prefs.getInt("stage", 2);
db.open();
String st=db.newword(3);
currentWord = st;
byte[] p=db.getpic(3);
Bitmap bm=BitmapFactory.decodeByteArray(p, 0, p.length);
img.setImageBitmap(bm);
break;
}
case 3:{
//stage = prefs.getInt("stage", 3);
db.open();
String st=db.newword(4);
currentWord = st;
byte[] p=db.getpic(4);
Bitmap bm=BitmapFactory.decodeByteArray(p, 0, p.length);
img.setImageBitmap(bm);
img.setImageBitmap(bm);
db.close();
break;
}
}
for (int i = 0; i < currentWord.length(); i++) {
dashes = dashes + "-";
}
txtDashes.setText(dashes);
for (Button btn: invisibleButtons) {
btn.setVisibility(View.VISIBLE);
}
invisibleButtons.clear();
nCorrect = 0;
}