Search code examples
iosobjective-ccocos2d-iphonecocos2d-iphone-3

CCNode Hud, not acting as Hud but "scrolling with scene"


Attempting to simply have a scene where I have the gameplay, and the hud. Since I have it positioning the scene (which contains both layers), I assume this is why my Menu button is moving even though it's in the Hud Layer. To fix it I would think that I would maybe have to change this line

[self setCenterOfScreen: mainChar.position];

to something like this:

[gameLayer setCenterOfScreen: mainChar.position];

But then I get this:

No visible @interface for CCNode declares the selector 'setCenterOfScreen:'

Here is the GameScene (commented where I think issue is):

+ (GameScene *)scene
{

    return [[self alloc] init];
}


// -----------------------------------------------------------------------

- (id)init
{
    self = [super init];
    if (!self) return(nil);

    CGSize winSize = [CCDirector sharedDirector].viewSize;

    self.userInteractionEnabled = YES;

    self.theMap     =   [CCTiledMap tiledMapWithFile:@"AftermathRpg.tmx"];
    self.contentSize = theMap.contentSize;

    CCNode *gameLayer = [CCNode node];
    gameLayer.userInteractionEnabled = YES;
    gameLayer.contentSize = self.contentSize;
    [self addChild:gameLayer];

    [gameLayer addChild:theMap z:-1];

    self.mainChar     = [CCSprite spriteWithImageNamed:@"mainChar.png"];
    mainChar.position = ccp(200,300);
    [gameLayer addChild:mainChar];

// POSSIBLY WHY BOTH LAYERS ARE SHIFTING, RATHER THEN HUD STANDING STILL, 
// I essentially want the gameLayer to be centered on screen, not both. 
// I tried [gameLayer setCenterOfScreen: mainChar.position] but got error posted above

        [self setCenterOfScreen: mainChar.position];



    CCNode *hudLayer = [CCNode node];
    hudLayer.userInteractionEnabled = YES;
    hudLayer.position = ccp(winSize.width * 0.9, winSize.height * 0.9);
    [self addChild:hudLayer];


    CCButton *backButton = [CCButton buttonWithTitle:@"[ Menu ]" fontName:@"Verdana-Bold" fontSize:18.0f];
    backButton.positionType = CCPositionTypeNormalized;
    backButton.position = ccp(0.85f, 0.95f); // Top Right of screen
    [backButton setTarget:self selector:@selector(onBackClicked:)];

    [hudLayer addChild:backButton];

    return self;
}

but then I just get:

Just confused on how I would set up a scene, to initially have 2 layers - one that keeps position in say the top right as a HUD, and the other that scrolls with scene for gameplay when telling it to.


Solution

  • This is related to the other question you asked here (Adding a Hud Layer to Scene Cocos2d-3) which I posted the answer to. In the future it is better to modify your original OP in your first question rather than create a new question that is almost the same.

    Hope this helped.