Fixed sprite on the tile map cocos2d?

154 Views Asked by At

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];

}
0

There are 0 best solutions below