1

当玩家到达“门”时,此代码会加载 map1.tmx tileMap。当玩家进入门时,它会加载一个新的 map2.tmx 文件。问题是当加载新的 map2.tmx 文件时,旧的 map1.tmx 在 map.2.tmx 后面运行并使用所有 map.1.tmx 墙壁碰撞并忽略 map2.tmx 墙壁碰撞。有没有办法像 removeObject 这样与 addObject 相反并将 map2.tmx 添加为新地图?我想让 map.2 在玩家进门时运行。

我尝试过 removeAllActions、removeAllChildren、removeTileAtCoord: 和其他方法,但我缺乏 SpriteKit 经验。任何帮助表示赞赏。

- (void)handleDoorCollisions:(Player *)player
{
if (self.doorOver) return;
NSInteger indices[8] = {7, 1, 3, 5, 0, 2, 6, 8};

for (NSUInteger i = 0; i < 8; i++) {
NSInteger tileIndex = indices[i];

CGRect playerRect = [player collisionBoundingBox];
CGPoint playerCoord = [self.doors coordForPoint:player.desiredPosition];

NSInteger tileColumn = tileIndex % 3;
NSInteger tileRow = tileIndex / 3;
CGPoint tileCoord = CGPointMake(playerCoord.x + (tileColumn - 1), playerCoord.y + (tileRow - 1));

NSInteger gid = [self tileGIDAtTileCoord:tileCoord forLayer:self.doors];
if (gid != 0) {
  CGRect tileRect = [self  tileRectFromTileCoords:tileCoord];
  if (CGRectIntersectsRect(playerRect, tileRect)) {
    [self.doors removeTileAtCoord:tileCoord];

    // add new map2.tmx
    self.map = [JSTileMap mapNamed:@"map2.tmx"];
    [self addChild:self.map];
    [self removeObject:????]

  }
}
}
}
4

1 回答 1

0

我遇到过同样的问题。我的解决方案是/正在加载地图时,我在 List<> 中收集所有碰撞数据,并针对该列表检查任何碰撞。当玩家进入不同的地图时,列表会被清除并使用新地图中的数据重新初始化。

我将这种方法用于其他一些事情,比如玩家可以与之交互的对象或触发某些动作的区域。

于 2018-03-29T11:16:39.470 回答