0

I can't figure out how to handle with tile map layer and other node. I want to make another one layer(CCNode) for such things as menu button, score, joystick that must always stay fixed(not scrolled), when the whole map is scrolled.

self.theMap=[CCTiledMap tiledMapWithFile:@"map.tmx"];
self.bgLayer=[theMap layerNamed:@"bg"];
  [self addChild:theMap z:-1];



  CCNode *joystickNode;
  joystickNode=[CCNode node];
  [bgLayer.parent addChild:joystickNode z:2];


upArrowFrame=[CCSpriteFrame frameWithImageNamed:@"uparrow.png"];
upArrow=[CCButton buttonWithTitle:@"" spriteFrame:upArrowFrame];
[upArrow setTarget:self selector:@selector(upArrowPressed)];
upArrow.position= ccp(190,190);
[joystickNode addChild:upArrow z:2];

Now upArrow is not visible on the screen at all. If I add to self instead of joystickNode, it will appear.

I can't even understand, what parent should new CCNode have. Can anyone explain it to me? I also tried to add new CCNode as a child of self and theMap.

EDIT: Oops, it's actually moving the camera. How to implement it in this case?

    -(void)setCenterOfScreen :(CGPoint) position {
    CGSize screenSize=[[CCDirector sharedDirector]viewSize];
    int x=MAX(position.x, screenSize.width/2);
    int y=MAX(position.y, screenSize.height/2);

    x= MIN(x, theMap.mapSize.width * theMap.tileSize.width - screenSize.width/2);
    y= MIN(y, theMap.mapSize.height * theMap.tileSize.height - screenSize.height/2);

    CGPoint goodPoint =ccp(x,y);
    CGPoint centerOfScreen = ccp(screenSize.width/2, screenSize.height/2);
    CGPoint difference = ccpSub(centerOfScreen, goodPoint);

    self.position=difference;


}

- (void)update:(CCTime)dt{


    [self setCenterOfScreen:hero.position];

}
  • If I get this right, bgLayer.parent is identical to theMap and thus the joystick is part of the map. If that's not the problem try adding the joystick directly to the scene. You should also post how you do the actual scrolling. – CodeSmile Oct 19 '14 at 17:17
  • @LearnCocos2D Oops, it's actually moving the camera. How to implement it in this case? –  Oct 19 '14 at 17:25
  • so basically you're changing the position of the CCScene object? In that case add another node that contains everything the scene currently contains (tilemap & scrolling code), then add the static, immovable nodes directly to the scene. Sprite Kit programming guide has an example, the principle can be applied to cocos2d as well. – CodeSmile Oct 19 '14 at 17:32
  • @LearnCocos2D I want to specify what "directly to the scene" means? Adding child to "self"? –  Oct 19 '14 at 18:44
  • if "self" refers to the SKScene instance, then yes, that's what I mean – CodeSmile Oct 21 '14 at 10:07

0 Answers0