我尝试制作游戏,我需要计算得分。分数在load()
方法中生成,应放在TextView score
。出现的问题是,我的textView没有变化,它始终保持不变:0
。
public class MainActivity extends Activity implements OnGestureListener {
private Paint paint = new Paint();
public int sco = 0;
public int colora;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
load();
TextView score = (TextView) findViewById(R.id.textView1);
score.setText(String.valueOf(sco));
}
private void load() {
Bitmap bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bg);
RelativeLayout ll = (RelativeLayout) findViewById(R.id.rect);
ll.setBackgroundDrawable(new BitmapDrawable(bg));
List < Integer > numbers = Arrays.asList(Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW);
Collections.shuffle(numbers);
colora = numbers.get(0);
if (colora == Color.RED) {
sco++;
}
paint.setColor(numbers.get(0));
canvas.drawRect(20, 15, 11, 3, paint);
paint.setColor(numbers.get(1));
canvas.drawRect(19, 15, 15, 3, paint);
paint.setColor(numbers.get(2));
canvas.drawRect(5, 5, 15, 35, paint);
paint.setColor(numbers.get(3));
canvas.drawRect(5, 15, 26, 1.4, paint);
}
}
答案 0 :(得分:1)
每次score.setText(String.valueOf(sco));
更改时都致电sco
。您只需在onCreate()
中调用一次。
答案 1 :(得分:1)
我会移动一份:
score.setText(String.valueOf(sco));
进入你的加载方法:
colora = numbers.get(0);
if (colora == Color.RED) {
sco++;
score.setText(String.valueOf(sco));
}
所以一切都将是:
public class MainActivity extends Activity implements OnGestureListener {
private Paint paint = new Paint();
public int sco = 0;
public int colora;
TextView score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
score = (TextView) findViewById(R.id.textView1);
score.setText(String.valueOf(sco));
load();
}
private void load() {
Bitmap bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bg);
RelativeLayout ll = (RelativeLayout) findViewById(R.id.rect);
ll.setBackgroundDrawable(new BitmapDrawable(bg));
List < Integer > numbers = Arrays.asList(Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW);
Collections.shuffle(numbers);
colora = numbers.get(0);
if (colora == Color.RED) {
sco++;
score.setText(String.valueOf(sco));
}
paint.setColor(numbers.get(0));
canvas.drawRect(20, 15, 11, 3, paint);
paint.setColor(numbers.get(1));
canvas.drawRect(19, 15, 15, 3, paint);
paint.setColor(numbers.get(2));
canvas.drawRect(5, 5, 15, 35, paint);
paint.setColor(numbers.get(3));
canvas.drawRect(5, 15, 26, 1.4, paint);
}
}