1. 相同位置放置2个卡片的Bug
2. 思路
卡片放置区域的各个位置有且仅有一个 mapIndex ,若出现重复则不再放置
2.1 卡片安放事件
package com.su.botanywarzombies.view;
public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
public void applay4Plant(int locationX, int locationY, EmplacePea emplacePea) {
synchronized (mSurfaceHolder) {
....
switch (raceIndex) {
case 0:
gameLayout4plant0.add(new Pea(point.x, point.y, key));
break;
case 1:
gameLayout4plant1.add(new Pea(point.x, point.y, key));
break;
case 2:
gameLayout4plant2.add(new Pea(point.x, point.y, key));
break;
case 3:
gameLayout4plant3.add(new Pea(point.x, point.y, key));
break;
case 4:
gameLayout4plant4.add(new Pea(point.x, point.y, key));
break;
2.2 初始化 Pea 对象尤其是 mapIndex
package com.su.botanywarzombies.entity;
public class Pea extends BaseModel {
public class Pea extends BaseModel {
private int farmeIndex = 0;
// 禁止一个位置放2个动画卡片
private int mapIndex;
public Pea(int locationX, int locationY, int mapIndex) {
this.locationX = locationX;
this.locationY = locationY;
isLive = true;
this.mapIndex = mapIndex;
}
@Override
public void drawSelf(Canvas canvas, Paint paint) {
super.drawSelf(canvas, paint);
if (isLive) {
canvas.drawBitmap(Config.peaFlames[farmeIndex], locationX, locationY, paint);
/* farmeIndex ++;
if (farmeIndex == Config.peaFlames.length -1) {
farmeIndex = 0;
}*/
farmeIndex = (++farmeIndex) % 8;
}
}
@Override
public int getMapIndex() {
return mapIndex;
}
这里实现了一个接口
package com.su.botanywarzombies.model;
public interface Plant {
public int getMapIndex();
}
public class BaseModel implements Plant{
// 卡片放置区域的 mapIndex
@Override
public int getMapIndex() {
return 0;
}
}
2.3 下次放置卡片的去重复判断
public void applay4Plant(int locationX, int locationY, EmplacePea emplacePea) {
synchronized (mSurfaceHolder) {
....
if (isExist(key, raceIndex)) {
return;
}
private boolean isExist(int key, int raceIndex) {
switch (raceIndex) {
case 0:
return hasMapIndex(key, gameLayout4plant0);
case 1:
return hasMapIndex(key, gameLayout4plant1);
case 2:
return hasMapIndex(key, gameLayout4plant2);
case 3:
return hasMapIndex(key, gameLayout4plant3);
case 4:
return hasMapIndex(key, gameLayout4plant4);
default:
break;
}
return false;
}
private boolean hasMapIndex(int key, ArrayList<BaseModel> list) {
for (BaseModel model : list) {
if (model instanceof Plant) {
if (key == model.getMapIndex()) {
return true;
}
}
}
return false;
}