1

this code loads a map1.tmx tileMap when player reaches the "door". When the player enters the door it loads a new map2.tmx file. Problem is when the new map2.tmx file is loaded, the old map1.tmx is running behind map.2.tmx and using all of map.1.tmx wall collisions AND ignoring map2.tmx wall collisions. Is there a way to do an opposite of addObject like removeObject and add map2.tmx as the new map? I would like to make map.2 run when the players enters the door.

I have tried removeAllActions, removeAllChildren, removeTileAtCoord: and other approaches but I lack of SpriteKit experience. Any help is appreciated.

- (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:????]

  }
}
}
}
user3078406
  • 511
  • 1
  • 9
  • 26
  • I don't see 'map1.tmx' anywhere in your code. – El Tomato Feb 21 '18 at 07:57
  • map.1.tmx runs as soon as the app loads. Please assume map1.tmx is already running in the back. When the new map loads in line code self.map = [JSTileMap mapNamed:@"map2.tmx"]; - map1.tmx is still running in the background – user3078406 Feb 21 '18 at 08:06
  • What about https://developer.apple.com/documentation/spritekit/sknode/1483091-removechildren, something like `[self removeChildren:{self.map}]` before assigning the new map (never coded ObjectiveC, so not sure if syntax is right). – Thorbjørn Lindeijer Feb 21 '18 at 08:42
  • Perfect! [self removeChildren:{self.map}] did the trick! Thank you so much! – user3078406 Feb 21 '18 at 08:58

1 Answers1

0

I had the same issue. My solution was/is on map load I collect all collision data in a List<> and check any collisions against this list. When the player enters a different map the list is beeing cleared and reinitialized with the data from the new map.

I use this approach for some other things as well like objects the player can interact with or areas that trigger some actions.

Pavel Slesinger
  • 498
  • 3
  • 12