gpt4 book ai didi

java - Android游戏3供用户试用

转载 作者:行者123 更新时间:2023-12-01 23:51:47 25 4
gpt4 key购买 nike

您好,这是我第一次开发 Android 色彩游戏。不过我想在每个问题中进行 3 次试验。我有点困惑如何或在哪里放置 while 循环在我的代码中。请看看我到目前为止所做的尝试:

  int trial = 0;

private void getCorrectObject() {

List<Integer> objects = new ArrayList<Integer>();
objects.add(1);
objects.add(2);
objects.add(3);
objects.add(4);
objects.add(5);
objects.add(6);
objects.add(7);
objects.add(8);
objects.add(9);

Collections.shuffle(objects);
int correctObject = objects.get(0);
Log.d("test", String.valueOf(correctObject));



while(trial <=3){
trial++;


switch(correctObject)
{
case 1:
bObjectCorrect.setImageResource(R.drawable.stage1_1_object1);
bObjectCorrect.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 2:
bObject1.setImageResource(R.drawable.stage1_1_object1);
bObject1.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 3:
bObject2.setImageResource(R.drawable.stage1_1_object1);
bObject2.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 4:
bObject3.setImageResource(R.drawable.stage1_1_object1);
bObject3.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 5:
bObject4.setImageResource(R.drawable.stage1_1_object1);
bObject4.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 6:
bObject5.setImageResource(R.drawable.stage1_1_object1);
bObject5.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 7:
bObject6.setImageResource(R.drawable.stage1_1_object1);
bObject6.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 8:
bObject7.setImageResource(R.drawable.stage1_1_object1);
bObject7.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
case 9:
bObject8.setImageResource(R.drawable.stage1_1_object1);
bObject8.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Stage1_2.class);
startActivity(i);
new Thread(){
public void run(){
MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
mp.start();
}
}.start();
finish();
}
});
break;
} // Last of switch statement


if(trial == 3){
new AlertDialog.Builder(this)
.setTitle("Game Over")
.setMessage("Sorry you reached your 3rd trial")
.setPositiveButton("Try Again?", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Stage1_2.this, Stage1_1.class);
startActivity(i);
}
})
.setNegativeButton("Back to Menu", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Stage1_2.this, ShapingColors.class);
startActivity(i);
}
})
.show();
}

} // end of while loop

我真的很想完成这件事,这样我就可以继续游戏了。非常感谢任何帮助。提前致谢。

我添加了一个名为guessedWrong()的新方法

 private void guessedWrong(){
trial++;

if(trial == 3){
new AlertDialog.Builder(this)
.setTitle("Game Over")
.setMessage("Sorry you reached your 3rd trial")
.setPositiveButton("Try Again?", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Stage1_1.this, Stage1_1.class);
startActivity(i);
}
})
.setNegativeButton("Back to Menu", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Stage1_1.this, ShapingColors.class);
startActivity(i);
}
})
.show();
}


}

最佳答案

您感到困惑的原因是您不能使用循环来完成此任务。使用 Java Android 框架时,每个回调函数(例如,onClick 监听器或 ActivityonResume)都必须在应用程序的一帧内运行并完成。仅在函数返回后,您的应用程序才会更新屏幕。这意味着,如果您正在执行诸如响应一系列点击之类的操作,则无法循环执行此操作。您必须在单独的回调调用中处理每次点击。如果您想迭代一个项目列表来决定现在应该发生什么(例如,如果您向 ListView 添加项目),但您无法迭代在不同时间发生的事情(例如用户的猜测)。

您必须将您的Activity视为状态机。将 Trial 设为 Activity 的成员变量(字段),从 0 开始。您可能有一个函数 guessedWrong(),它会递增 Trial,如果大于 2,则转到“游戏结束”屏幕。onClick错误答案的监听器将调用此函数。转到新问题时,请将试用重置为0

您还需要确保在Activity重新启动时保留试验次数(您所处的状态)。类(class)Recreating an ActivityAndroid Developers' Training Google 提供的功能向您展示了如何执行此操作。

关于java - Android游戏3供用户试用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16219481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com