可以从地图上移除宝箱以使其不再被收集,但不能通过编辑 TMX 地图。为了做到这一点,每当玩家走过一个箱子时(通过向箱子添加一个属性来检查,例如 chest=true 然后检查它),除了奖励玩家之外,您还必须做一些事情,那就是使用共享偏好保存箱子有使用字符串集(例如,使用键“chests”)并包含坐标,用“:”分隔。保存坐标:
String saveMe = tileRow + ":" + tileColumn;
removeChest(tileRow, tileColumn);
加载坐标:
String loaded = loadString();
String[] coords = loades.split(":");
tileRow = Integer.parseInt(coords[0]);
tileColumn = Integer.parseInt(coords[1]);
removeChest(tileRow, tileColumn);
现在您可以保存/加载使用过的箱子。这是每当玩家走过具有 (chest=true) 属性的图块时:
boolean found = false;
for (int i = 0; i < chestsUsedTileRowsArray.length; i++) {
if (chestFoundTileRow == chestsUsedTileRowsArray[i] && chestFoundTileColumn == chestsUsedTileColumnsArray[i]) {
found = true;
break;
}
}
if (!found) {
rewardPlayer();
saveChestUsed(tileRow, tileColumn);
}
最后,removeChest() 需要一个小技巧:在胸部绘制一个具有地面纹理的精灵:
void removeChest(int tileRow, int tileColumn) {
final TMXTile tileToReplace = tmxMap.getTMXLayers().get(0).getTMXTile(tileColumn, tileRow);
final int w = tileToReplace.getTileWidth();
final int h = tileToReplace.getTileHeight();
Sprite sprite = new Sprite(w * (tileColumn + 0.5), h * (tileRow + 0.5), textureRegionOfGround, this.getVertexBufferObjectManager());
scene.addChild(sprite);
}