5秒后关闭Android游戏

时间:2014-10-14 13:06:06

标签: android eclipse handler

我想建立一个简单的安卓游戏。

用户回答问题,当答案正确时,它会继续..

我想为每个答案添加时间控制。

我尝试添加处理函数,但我没有。

我的代码;

import java.util.Collections; 
import java.util.Arrays; 
import java.util.List; 
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class EasyGameActivity extends Activity {
public int score = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);       

    setContentView(R.layout.activity_easygame);

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
            finishScreen();
        }
    }, 5000);       

    startGame();
}

private void startGame() {
    // TODO Auto-generated method stub

    Button b1 = (Button)findViewById(R.id.answer_one);
    Button b2 = (Button)findViewById(R.id.answer_two);
    Button b3 = (Button)findViewById(R.id.answer_three);
    Button b4 = (Button)findViewById(R.id.answer_four);

    Random number = new Random();
    int first = number.nextInt(100)+1;
    int second = number.nextInt(100)+1;
    int answer = first + second;
    int rnd1 = answer + 1;
    int rnd2 = answer + 2;
    int rnd3 = answer - 1;

    final String a = Integer.toString(answer);
    String b = Integer.toString(rnd1);
    String c = Integer.toString(rnd2);
    String d = Integer.toString(rnd3);


    ((TextView) findViewById(R.id.display)).setText(Integer.toString(first) + '+' + Integer.toString(second));

    List<Button> buttons = Arrays.asList(b1, b2, b3, b4);
    List<String> texts = Arrays.asList(a, b, c, d);
    Collections.shuffle(texts);
    int i = 0;
    OnClickListener onClick = new OnClickListener() {
        public void onClick(View view) {
            Button button = (Button) view;
            String value = (String) button.getText();

            if(value == a) {
                checkTrue();
            } else {
                finishScreen();
            }
        }       
    };
    for(Button button : buttons) {
        button.setText(texts.get(i++));
        button.setOnClickListener(onClick);
    }
}

private void checkTrue() {
    score++;
    ((TextView) findViewById(R.id.score)).setText(Integer.toString(score));
    startGame();
}

private void finishScreen() {
    score = 0;
    startActivity (new Intent("com.bsinternet.mathfast.RESTARTGAMESCREEN"));        
    finish();
}

}

如何添加时间控制。感谢。

1 个答案:

答案 0 :(得分:1)

这段代码看起来不正确

if(value == a) {
   checkTrue();
} else {
   finishScreen();
}

您应该使用equals()来检查字符串是否相等。目前,您只检查对象相等性,评估为False,代码永远不会调用checkTrue()

请改为:

if(value.equals(a) {
   checkTrue();
} else {
   finishScreen();
}