我正在这个小游戏中,每个500毫秒的图像设置为图像视图 所以我想检查的是当我点击其中一个箭头图像(左,右,上,下),以检查它是否等于随机图像...如果是的话1。 这里的分数增加是我的布局 https://i.stack.imgur.com/wPxZB.png检查图像设置为图像视图每500毫秒
,这里是我的代码
package com.andreh.catchthatarrow; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import java.util.Timer; import java.util.TimerTask; public class GameActivity extends AppCompatActivity { ImageView img_up; ImageView img_down; ImageView img_left; ImageView img_right,imgRand; private int time = 600; private static int SCORE = 0; private TextView score; int pos = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game); score = (TextView)findViewById(R.id.textView3); img_up = (ImageView)findViewById(R.id.imgUp); img_down = (ImageView)findViewById(R.id.imgDown); img_left = (ImageView)findViewById(R.id.imgLeft); img_right = (ImageView)findViewById(R.id.imgRight); imgRand = (ImageView)findViewById(R.id.changeable); final int [] arrBuckets = {R.drawable.arrow_left,R.drawable.arrow_right,R.drawable.up,R.drawable.down}; Timer mTimer = new Timer(); mTimer.schedule(new TimerTask() { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { pos = arrBuckets[(int) (Math.random() * arrBuckets.length)]; imgRand.setImageResource(pos); if(time>10 || time<20) time=100; } }); } },0,time); img_up.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(pos==2){ SCORE++; score.setText(SCORE+""); } } }); img_down.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(pos==3){ SCORE++; score.setText(SCORE+""); } } }); img_left.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(pos==0){ SCORE++; score.setText(SCORE+""); } } }); img_right.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(pos==1){ SCORE++; score.setText(SCORE+""); } } }); } }
我应该告诉你,你的设置,而不是让这些图像是从ImageView的使用自己随机函数图像, ImageView类可以将这些信息保留在片段类中,然后相应地增加分数。 –