2

Im working on a little game in IOS which involves a scrolling tilemap. I have gotten my background picture to scroll like so

- (void)moveBg
{
  [self enumerateChildNodesWithName:@"scroll" usingBlock:
   ^(SKNode *node, BOOL *stop) {
     SKSpriteNode * bg = (SKSpriteNode *) node;
     CGPoint bgVelocity = CGPointMake(-BG_POINTS_PER_SEC, 0.0);
     CGPoint amtToMove =     CGPointMultiplyScalar(bgVelocity, _dt);
     bg.position = CGPointAdd(bg.position, amtToMove);
   }];
}

however if i load my tilemap and name it "scroll" as i have below

- (TileMapLayer *)createLandScape
{

  _tileMap = [JSTileMap mapNamed:@"level1.tmx"];
  _tileMap.name=@"scroll";
  return [[TmxTileMapLayer alloc]
          initWithTmxLayer:[_tileMap layerNamed:@"Background"]];
}

I am lead to believe that tilemap scrolling is then different from background image scrolling. Id like if someone could help me or point me to the right direction ton accomplish ths.

Thanks!

Jennifer
  • 89
  • 7
  • Is it possible to assign the tilemap unto a background? and scroll it? – Jennifer Apr 18 '14 at 20:26
  • What exactly do you mean by scrolling the map? Are you talking about the map moving as your player moves around the map? – sangony Apr 18 '14 at 20:31
  • @sangony no, there is no player, I want the tilemap to scroll left to right, like I would scroll a background. – Jennifer Apr 19 '14 at 03:33
  • @sangony on it's own...not following a player – Jennifer Apr 19 '14 at 04:04
  • 1
    @sangony I have been trying to run actions on the tilemap but that doesnt seem to work either – Jennifer Apr 19 '14 at 13:24
  • I think it's more a case of moving the view rather than moving the tile map. Have you considered and tried doing that instead? – sangony Apr 19 '14 at 14:15
  • @sangony Have considered but, wont that move all my children of the view as well? secondly, is the principle of moving a view the same as moving a background? – Jennifer Apr 19 '14 at 18:14
  • Yes and no. The background is usual set at 0,0 and the view shifts around the background. So it is easier to move the view and keep track of everything on the background, at least in my humble opinion. If you have children attached to the view then they will move too but usually that's only the player. Other objects usually have defined coordinates relative to the background map instead of the view. Example: an enemy appears on the view when the view reaches y=800. My suggestion is by no means the only right one as it really all comes down to how you write your code. – sangony Apr 19 '14 at 18:22
  • @sangony OK, so in that case....if i make the tilemap a child of the background picture and scroll the background pic. the tilemap should scroll as well...right? – Jennifer Apr 19 '14 at 18:32
  • Yes it should. If you still run into problems let me know and I can post some sample code for you. – sangony Apr 19 '14 at 18:36
  • @sangony going to try it now – Jennifer Apr 19 '14 at 18:36
  • @sangony ah yes indeed. my tilemap scrolled, could you post something under answer so i can select it as answer? – Jennifer Apr 19 '14 at 18:46
  • Thanks for the offer but you solved this one by yourself. Post your answer, mark it as correct and I'll give it a +1. Happy coding! – sangony Apr 19 '14 at 19:01

2 Answers2

2

Added a background of SKNode and added the tilemap as a child. Now when the scroll background code is called it scrolls the tilemmap along with is

  SKSpriteNode * bg =
  [SKSpriteNode spriteNodeWithImageNamed:@"bg"];
  bg.anchorPoint = CGPointZero;
  bg.position = CGPointZero;
  bg.name = @"bg";
  [self addChild:bg];
    [bg addChild:_tileMap];
Jennifer
  • 89
  • 7
1

Since the JSTileMap extends from SKNode, you should be able to apply actions as if you would any other node.

Anyway, you're casting your tilemap into an SKSpriteNode. Not only that, but you're also wrapping your tilemap to the TmxTileMapLayer class. Not sure why you're doing this, but the problem is that your JSTileMap is out of scope once you cast it.

You might want to try this:

- (void)moveBg
{
  [self enumerateChildNodesWithName:@"scroll" usingBlock:
   ^(SKNode *node, BOOL *stop) {
     JSTileMap * bg = (JSTileMap *) node;
     CGPoint bgVelocity = CGPointMake(-BG_POINTS_PER_SEC, 0.0);
     CGPoint amtToMove =     CGPointMultiplyScalar(bgVelocity, _dt);
     bg.position = CGPointAdd(bg.position, amtToMove);
   }];
}

You might also be able to move your entire wrapper class by casting it as well (if TmxTileMapLayer extends from SKNode)

TmxTileMapLayer * bg = (TmxTileMapLayer *) node;

A different way of scrolling your tilemap is just as simple as:

_tiledMap = [JSTileMap mapNamed:@"level1.tmx"];
if (_tiledMap) {
    [self addChild:_tiledMap];
}
_tiledMap.position = CGPointMake(ORIGINPOINT);

SKAction *scroll = [SKAction moveTo:CGPointMake(MOVETOPOINT) duration:SPEED];
[_tiledMap scroll];

And even better way (move a map layer instead of the entire tileMap):

_tiledMap = [JSTileMap mapNamed:@"level1.tmx"];
if (_tiledMap) {
    [self addChild:_tiledMap];
}
_tiledMap.position = CGPointMake(ORIGINPOINT);

TMXLayer *someLayer = [_tiledMap layerNamed:@"someLayer"];

SKAction *scroll = [SKAction moveTo:CGPointMake(MOVETOPOINT) duration:SPEED];
[someLayer scroll];
JRam13
  • 1,132
  • 1
  • 15
  • 25