所以我有一个程序生成的游戏,加载成块,我需要一种方法让我的代码记住,如果我砍下一棵树或打破一块岩石,所以它不会取代它离开和进入一个新的区域。我得到它几乎工作,除了不将它们放回正确的位置,它移动它们填补空白。 (如果是这样的话)
这是将删除的瓷砖添加到我移除的瓷砖阵列中的简洁功能
public function removeAndAddTile(tileRemoved:Function, tileAdded:Function, i:int)
{
world.removedTiles.push(world.tilesInWorld[i]);
}
这是在进入区域后放置瓷砖后加载的内容。换句话说,什么是假设要替换已被销毁的瓷砖
protected function usedTiles()
{
for (var i:int = world.tilesInWorld.length - 1; i >= 0; --i)
{
for (var c:int = world.removedTiles.length - 1; c >= 0; --c)
{
if (world.tilesInWorld[i].x == world.removedTiles[c].x && world.tilesInWorld[i].y == world.removedTiles[c].y)
{
var ti:String = world.tilesInWorld[i].onTile;
var tx:int = world.removedTiles[c].x;
var ty:int = world.removedTiles[c].y;
world.worldTiles.removeChild(world.tilesInWorld[i]);
switch (ti)
{
case "grass" :
world.tilePool.returnSprite(world.tilesInWorld[i]);
break;
case "stone" :
world.rockPool.returnSprite(world.tilesInWorld[i]);
break;
}
world.tilesInWorld.splice(i, 1);
clicked = true;
switch (ti)
{
case "grass" :
world.tile = world.tilePool.getSprite();
break;
case "stone" :
world.tile = world.stonePool.getSprite();
break;
}
world.tile.width = world.TILE_SIZE;
world.tile.height = world.TILE_SIZE;
world.tile.x = tx;
world.tile.y = ty;
world.tilesInWorld.push(world.tile);
world.worldTiles.addChildAt(world.tile, i);
}
}
}
}
我的问题很简单,我怎样才能让我的瓷砖在被销毁时记住,并在进入某个区域时删除并更换这些瓷砖?
答案 0 :(得分:1)
您可以使用两个图层。地图表面的一个底层,另一个细节层包含树木,岩石。当你的角色移动并出现新的区域图块时,尝试获取新区域图块数据并将树木,岩石添加到新图块位置。
//the key may be tileX_tileY, the value will be the object type
//if a rock or a tree been broke,delete detailDic[tileX_tileY]
var detailDic:Dictionary;
当您的角色移动并进入新区域时
var newTiles:Array;//the new appeared tiles
for each (var tile:Tile in newTiles)
{
var key:String = tile.tileX + "_" + tile.tileY;
var objectType:String = detailDic[key];
if (check ojectType is valid)
{
add the wanted tree, rock to target position.
}
}
//here you should remove the trees and rocks that disappear from your map's ViewPort