我正在研究这个小型游戏,每500毫秒将一个图像设置为图像视图 所以我要检查的是当我点击其中一个箭头图像(左,右,上,下)来检查它是否等于随机图像...如果是,则分数增加1。 这是我的布局 https://i.stack.imgur.com/wPxZB.png
这是我的代码
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+""); } } }); } }
答案 0 :(得分:0)
您正在检查索引中的数据,并且您需要该位置,因此请改用:
pos = Array.asList(arrayBuckets).indexOf((int) (Math.random() * arrBuckets.length));
img_up.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
if(pos==2)
{
SCORE++;
score.setText(SCORE+"");
}
});