我正在這個小遊戲中,每個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類可以將這些信息保留在片段類中,然後相應地增加分數。 –