android - 如何在 2x2 网格布局中实现原生 android 库以在一秒钟内检测触摸?
问题描述
屏幕元素;
TextView shows game score
Rest of the screen is divided into 4 equal colored rectangle views
目标
Per second any of those 4 rectangle views will change its color to gray
the gray color will remain for 1 whole second
after 1 second the gray rectangle will revert to its original color and again one random rectangle will turn gray
player needs to tap on the rectangles that are grey during that 1 second, so that there is at least 1 tap per second
条件
if user taps on the rectangle view while gray then score moves up by 1+
if user cannot tap the view within 1 second, game over
if user taps on a view that is not gray, also game over
简而言之,目标是在一秒钟内点击灰色矩形。如果错过或点击彩色框,则游戏结束。
我试着像下面这样编码,我用字母代替颜色,用 X 代替灰色,
请帮助我理解我做错了什么?
任何帮助表示赞赏。
谢谢!
public class MainActivity extends AppCompatActivity {
Button ba, bb, bc, bd, startgame;
TextView score;
GameCountDownTimer gameCountDownTimer;
// stores button index, original color
int greyButton[] = new int[2];
int clicked = 99;
boolean firstrun=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startgame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gameCountDownTimer = new GameCountDownTimer(3000, 1000);
gameCountDownTimer.start();
}
});
}
public class GameCountDownTimer extends CountDownTimer {
public GameCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
int progress = (int) (millisUntilFinished/1000);
score.setText(String.valueOf(progress));
}
@Override
public void onFinish() {
if(firstrun) {
randomize();
firstrun=false;
}
else{
// only continue if player taps on the correct button
// or clicked == greyButton[0]
if(clicked==greyButton[0]){
randomize();
this.start();
} else {
score.setText("GAME OVER!");
this.cancel();
}
}
}
}
public void randomize(){
// if grayButton has a value, revert to original color
if(greyButton != null){
revertToOriginalColor(greyButton[0]);
}
int nextGrayButton = new Random().nextInt(4);
setGrayButton(nextGrayButton);
}
public void revertToOriginalColor(int index){
}
public void setGrayButton(int index){
}
}
编辑: 解决
onCreate()
在我的活动类中实现 View.OnClickListener 后,在我的活动类中添加了以下部分并将所有 4 个按钮注册到覆盖的 onClick。
// initialize the CountDownTimer
timer = new CountDownTimer(1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
//updating the time
}
@Override
public void onFinish() {
gameOver = true;
this.cancel();
simpleAlert(score);
}
};
解决方案
我建议使用 Rxjava。在您的应用级别 build.gradle 文件中添加以下依赖项
implementation "io.reactivex.rxjava2:rxjava:2.1.9"
implementation "io.reactivex.rxjava2:rxandroid:2.0.1"
implementation "com.jakewharton.rxbinding2:rxbinding:2.0.0"
在您的课程中添加以下代码。
RxView.clicks(my_grid_layout)
.map(new Function<Object, Integer>() {
@Override
public Integer apply(Object o) throws Exception {
return 1;
}
})
.buffer(1, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new Observer<List<Integer>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(List<Integer> integers) {
Log.e(TAG, "onNext: " + integers.size() + " taps received!");
int number_of_clicks= integers.size();
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError: " + e.getMessage());
}
@Override
public void onComplete() {
Log.e(TAG, "onComplete");
}
});
如果你想限制只使用 android 库
private int count = 0;
private Timer myTimer;
relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
count= count+1;
}
});
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
Log.d("TAG",String.valueOf(count));
count=0;
}
}, 0, 1000);
}
推荐阅读
- file - 使用 php mysql 将 doc 文件文本拆分为多行
- javascript - 如何编写检查句子中字符数的 JavaScript?
- html - R删除字符串中通配符周围的字符
- r - R:无法在 PopOS 21.04 中安装天才包
- android - 我无法使用 android 上传带有 expo-file-picker 和 express-fileupload 的图像
- reactjs - 使用 useQuery 过滤数据 onChange 无法正常工作
- mysql - SQL 获取最近加入的记录
- javascript - 为什么 Mongoose 插入 null 作为子文档数组的最后一个元素?
- javascript - 使用键盘箭头键更改图像的功能
- vuejs3 - Vue 3:如何在没有路由器的情况下在当前页面链接上设置活动类?