【发布时间】:2020-08-16 21:09:11
【问题描述】:
我想做一个游戏,如果我按下播放按钮,随机关卡(活动)将打开。我得到了这个代码:https://stackoverflow.com/a/29579373/13101103 这是有效的,但我想编辑,例如,所有级别都有 2 个不同的答案,answer1 失败,answer2 通过级别,如果用户通过 level1,并且在 level2 失败,则返回 mainactivity,如果重新开始,则通过的关卡将不再显示。
示例: 有5个关卡,用户启动随机关卡,例如level3,它通过了,进入下一个随机关卡,例如level2,它通过,进入下一个... level4,它失败了,回到mainactivity,用户重新开始,但是已经通过的关卡不会显示,只会显示未通过的...示例开始level3...如果通过则转到level1....
如何为我的解决方案编辑此代码?有人可以给我一些提示吗?因为在这种情况下,如果我回到 mainactivity 并重新开始,那么它会从所有级别开始......我试图编辑,但我被卡住并且无法工作......
另外我想在用户离开应用程序时保存进度。在 sharedpreferences 中如何保存传递的级别(arraylist)......?
主活动:
enter code here
Button level1Button = findViewById(R.id.level1Button);
level1Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// We are creating a list, which will store the activities that haven't been opened yet
ArrayList<Class> activityList = new ArrayList<>();
activityList.add(Level1Activity.class);
activityList.add(Level2Activity.class);
activityList.add(Level3Activity.class);
activityList.add(Level4Activity.class);
activityList.add(Level5Activity.class);
Random generator = new Random();
int number = generator.nextInt(5) + 1;
Class activity = null;
// Here, we are checking to see what the output of the random was
switch(number) {
case 1:
activity = Level1Activity.class;
// We are adding the number of the activity to the list
activityList.remove(Level1Activity.class);
break;
case 2:
activity = Level2Activity.class;
activityList.remove(Level2Activity.class);
break;
case 3:
activity = Level3Activity.class;
activityList.remove(Level3Activity.class);
break;
case 4:
activity = Level4Activity.class;
activityList.remove(Level4Activity.class);
break;
default:
activity = Level5Activity.class;
activityList.remove(Level5Activity.class);
break;
}
// We use intents to start activities
Intent intent = new Intent(getBaseContext(), activity);
// `intent.putExtra(...)` is used to pass on extra information to the next activity
intent.putExtra("ACTIVITY_LIST", activityList);
startActivity(intent);
}
});
Level1Activity:
enter code here
failbutton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
ArrayList<Class> activityList = new ArrayList<>();
activityList.add(Level1Activity.class);
Bundle extras = getIntent().getExtras();
activityList = (ArrayList<Class>) extras.get("ACTIVITY_LIST");
//Class activity = null;
Intent intent = new Intent(Level1Activity.this, Main2Activity.class);
intent.putExtra("ACTIVITY_LIST", activityList);
startActivity(intent);
}
});
buttonlevel1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<Class> activityList = new ArrayList<>();
Bundle extras = getIntent().getExtras();
activityList = (ArrayList<Class>) extras.get("ACTIVITY_LIST");
if(activityList.size() == 0) {
// Do something when after all activities have been opened
//startActivity(new Intent(Level1Activity.this, Main2Activity.class));
//Intent intent = new Intent(Level1Activity.this, Main2Activity.class);
//intent.putExtra("ACTIVITY_LIST", activityList);
//startActivity(intent);
} else {
// Now, the random number is generated between 1 and however many
// activities we have remaining
Random generator = new Random();
int number = generator.nextInt(activityList.size()) + 1;
Class activity = null;
// Here, we are checking to see what the output of the random was
switch(number) {
case 1:
// We will open the first remaining activity of the list
activity = activityList.get(0);
// We will now remove that activity from the list
activityList.remove(0);
break;
case 2:
// We will open the second remaining activity of the list
activity = activityList.get(1);
activityList.remove(1);
break;
case 3:
// We will open the third remaining activity of the list
activity = activityList.get(2);
activityList.remove(2);
break;
case 4:
// We will open the fourth remaining activity of the list
activity = activityList.get(3);
activityList.remove(3);
break;
default:
// We will open the fifth remaining activity of the list
activity = activityList.get(4);
activityList.remove(4);
break;
}
// Note: in the above, we might not have 3 remaining activities, for example,
// but it doesn't matter because that case wouldn't be called anyway,
// as we have already decided that the number would be between 1 and the number of
// activities left.
// Starting the activity, and passing on the remaining number of activities
// to the next one that is opened
Intent intent = new Intent(getBaseContext(), activity);
intent.putExtra("ACTIVITY_LIST", activityList);
startActivity(intent);
}
}
});
level2, level3.... 相同只是 id-s 不同
【问题讨论】:
标签: java android random android-activity