我正在 Canvas 上制作类似 Battleships 游戏的东西。让我们看看领域:
问题是:当我按下另一个单元格时,黄色的单元格将再次变为黑色。我想保存它的状态,这样这个黄色单元格就不会再变黑了。
这是被击中时要做什么的代码:
// it's some method A
case 1:
drawHit = true;
this.draw_x = x;
this.draw_y = y;
invalidate();
break;
和
// It's in onDraw() method
if (drawHit == true) {
Log.d(TAG, "drawHit! drawX = " + draw_x + ", drawY = " + draw_y);
Paint ship = new Paint();
ship.setColor(getResources().getColor(R.color.ship_color));
Rect r = new Rect(draw_x*rebro_piece, draw_y * rebro_piece, (draw_x+1) * rebro_piece, (draw_y+1)*rebro_piece);
canvas.drawRect(r, ship);
drawHit = false; }