简体   繁体   English

AndEngine动态更改TMX平铺地图

[英]AndEngine change TMX Tiled Map Dynamically

I have read all the possible duplicates of this question and none gives me a full solution (the solution is in divided into the answers) so I decided to try and clear the things up. 我已经阅读了该问题的所有可能重复内容,但没有一个给我完整的解决方案(解决方案分为答案),因此我决定尝试解决问题。 BTW StackOverflow told me: BTW StackOverflow告诉我:

Not the answer you're looking for? 不是您要找的答案? Browse other questions tagged android andengine tmx or ask your own question. 浏览标记为android andengine tmx的其他问题,或询问您自己的问题。

and

It's OK to Ask and Answer Your Own Questions 可以提问并回答自己的问题
So [...] if you'd like to document it in public so others (including yourself) can find it later 因此,如果您想公开记录它,以便其他人(包括您自己)以后可以找到它。

Now this being clear, I would like to change a TMX map dynamically. 现在已经很清楚了,我想动态更改TMX映射。 For example the map has a chest object. 例如,地图有一个胸部对象。 The player walks on it and receives gold. 玩家在上面行走并获得金牌。 Then I would like to remove the chest from the map so the player cannot collect the chest more than once. 然后,我想从地图上删除箱子,以使玩家不能一次以上收集箱子。 How do I do this? 我该怎么做呢?

Removing the chest from the map so it can no longer be collected is possible, but not by editing the TMX Map. 可以从地图上移除箱子,使其无法再被收集,但是不能通过编辑TMX地图来进行。 To accomplish this, whenever the player walks over a chest (check by adding a property to the chest like chest=true and then checking it), besides rewarding the player, you must do something, and that is saving using Shared Preferences which chests have been used using a String Set (eg. with the key "chests") and containing the coordinates, separated by ":". 要做到这一点,每当玩家经过一个箱子时(通过在箱子上添加一个属性,例如Chest = true然后检查它),除了奖励玩家之外,您还必须做一些事情,并且使用共享的偏好来保存该箱子拥有的东西通过字符串集(例如,使用键“ chests”)使用并包含以“:”分隔的坐标。 To save the coordinates: 保存坐标:

String saveMe = tileRow + ":" + tileColumn;
removeChest(tileRow, tileColumn);

To load the coordinates: 加载坐标:

String loaded = loadString();
String[] coords = loades.split(":");
tileRow = Integer.parseInt(coords[0]);
tileColumn = Integer.parseInt(coords[1]);
removeChest(tileRow, tileColumn);

Now you can save / load used chests. 现在您可以保存/加载用过的箱子。 This is whenever the player walks over a tile which has (chest=true) property: 每当玩家走过具有(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);
}

Finally, there is removeChest() which requires a little trick: drawing a sprite which has the texture of the ground on the chest: 最后,有一个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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM