我试图制作一个简单的气泡游戏,我应该在屏幕上随机出现10个气泡,每个3秒。每当用户触摸一个泡泡时,他就会消失并获得一分。
现在的问题是我无法及时随机显示气泡,我试图在for循环中使用Thred.sleep()
但它会使所有应用程序等到循环结束然后它显示所有10个气泡。
这是java代码:
注意:请查看' generateButtons' 方法
package com.adil.bullegame;
import android.app.ActivityManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import static java.lang.Integer.parseInt;
import static java.lang.Thread.sleep;
import java.util.*;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
public class MainActivity extends AppCompatActivity {
private int bubblesNumber ;
static int clickedBubbles=0;
TextView text1;
private static String in;
private static final String FORMAT = "%02d:%02d:%02d";
int seconds , minutes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1=(TextView)findViewById(R.id.textView3);
CountDownTimer start = new CountDownTimer(10000, 1000) { // adjust the milli seconds here
public void onTick(long millisUntilFinished) {
text1.setText("" + String.format(FORMAT,
TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(
TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
showSimplePopUp();
clickedBubbles=0;
}
}.start();
// dynamic number of bubbles
in =null;
try{
InputStream is = getAssets().open("file.txt");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
in = new String(buffer);
} catch (IOException e) {
e.printStackTrace();
}
// bubblesNumber = parseInt(in);
generateButtons(10);
}
private void generateButtons(int nbr)
{
for(int i = 0; i< nbr ; i++)
{
createButton();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void createButton() {
RelativeLayout layout = (RelativeLayout)findViewById(R.id.activity_main);
final Button myButton = new Button(this);
layout.addView(myButton); ;
RelativeLayout.LayoutParams absParams =
(RelativeLayout.LayoutParams)myButton.getLayoutParams();
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int height = displaymetrics.heightPixels;
Random r = new Random();
absParams.leftMargin = r.nextInt(width-150) ;
absParams.topMargin = r.nextInt(height-150);
myButton.setLayoutParams(absParams);
myButton.postDelayed(new Runnable()
{ public void run()
{ myButton.setVisibility(View.GONE); } }, 3000);
//new Timer().schedule(new TimerTask() {
// @Override
// public void run() {
// ((LinearLayout)myButton.getParent()).removeView(myButton);
// myButton.setVisibility(View.GONE);
// }
// }, 3000);
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
clickedBubbles++;
v.setVisibility(View.GONE);
}
});
}
private void showSimplePopUp() {
AlertDialog.Builder gameOver = new AlertDialog.Builder(this);
gameOver.setTitle("GameOver");
gameOver.setMessage("You clicked "+ clickedBubbles + " bubbles");
gameOver.setPositiveButton("restart",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
}
});
AlertDialog dialog = gameOver.create();
dialog.show();
}
}
注意:请查看&#39; generateButtons&#39; 方法
答案 0 :(得分:0)
您的应用程序是否冻结,因为您正在主UI线程上调用sleep函数。有很多方法可以解决这个问题。
尝试以下方法
private void generateButton(final int numberOfButtons) {
new android.os.Handler().postDelayed(new Runnable() {
@Override
public void run() {
createButton();
if((numberOfButtons-1)>0){
generateButton(numberOfButtons-1);
}
}
}, 2000);
}