0

I have two layers set up like so in one scene:

header file for scene:

@interface GameScene1 : CCScene {

GameLayer *gameLayer;

HUDLayer *hudLayer;

}

Main file for scene:

-(id)init {

self = [super init];

if (self != nil) {

gameLayer = [GameLayer node];

[self addChild:gameLayer];

hudLayer = [HUDLayer node];

[self addChild:hudLayer];

}

return self;

}

HUD layer header:

@interface HUDLayer : CCNode {

CCSprite *background;

CGSize screenSize;



}
-(void)updateMonstersSlayed:(NSString*)value;

HUD layer main:

@implementation HudLayer
-(id)init
{
    self = [super init];

    if (self)
    {
        CGSize viewSize = [[CCDirector sharedDirector] viewSize];


        monstersSlayed = [CCLabelTTF labelWithString:@"Monsters Killed: 0" fontName:@"Arial" fontSize:15];
        monstersSlayed.position = ccp(viewSize.width * 0.85, viewSize.height * 0.1 );
        [self addChild:monstersSlayed];


    }
    return self;
}
-(void)updateMonstersSlayed:(NSString*)value
{
    monstersSlayed.string = value;
}

Game Layer main

- (BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair collisionPlayer:(CCNode *)user collisionMonster:(CCNode *)monster
{
    if (holdingWeapon)
    {

        HudLayer *myHud = [[HudLayer alloc] init];
        [myHud updateMonstersSlayed:@"Monsters Killed: 1"];

    }
}

Simply trying to get it set to where I can set text from the Game Layer to show up in a Label in the Hud Layer.

How would I accomplish this in Cocos2d 3?

  • Cocos2d V3 does not have CCLayer, it was removed. Regarding communcation between 2 "layers", since the scene will have a pointer reference to each of the 2 child nodes, you could use the CCScene as a controller (I can expand this if it sounds too vague). – lucianomarisi Jun 13 '14 at 15:53
  • By layer i meant 'Nodes' now, you're right sorry. As far as using the CCScene as a controller, I don't see how that would work if my gameplay methods and stuff are in my Game Layer node, and the label I want to set is in my Hud Layer Node. – user3727511 Jun 13 '14 at 15:58

3 Answers3

0

You can use methods. Make a method in HUD layer class.

-(void) giveMeMyText:(NSString*)someText
{
   do something with my someText
}

Don't forget to make the method visible in HUD.h -(void) giveMeMyText; Then import HUD layer class in GameScene1 #import "HUDlayer.h" and use it.

HUDLayer* myHud = [[HUDLayer alloc] init];
[myHud giveMeMyText:@"say hi!"];
denis alenti
  • 78
  • 1
  • 6
  • I tried this, and it doesn't seem to set the text. I will update my initial post to what I've tried – user3727511 Jun 13 '14 at 16:05
  • updated, any idea why it wouldn't work? Only guess I would have is that calling the alloc init on HudLayer is allocating a new one, rather then using the currently displayed one in CCScene? If that makes any sense... sorry I'm new to this but that would be the only reason why it wouldn't be working in my opinion – user3727511 Jun 13 '14 at 16:36
0

You could use delegation, so your scene would implement GameLayerProtocol and the delegate of GameLayer. That way the scene would be notified of any changes the GameLayer has and act appropriately on the HudLayer.

For example:

// GameLayer class

@protocol GameLayerProtocol <NSObject>

- (void)someThingHappenedInGameLayer;

@end

@interface GameLayer : CCNode

@property (nonatomic, weak) id<GameLayerProtocol> delegate;

@end

@implementation GameLayer

- (void)someActionInGameLayer
{
    [self.delegate someThingHappenedInGameLayer];
}

@end

// Scene class

@interface IntroScene : CCScene <GameLayerProtocol>

@end

@implementation IntroScene

// Implement protocol methods
- (void)someThingHappenedInGameLayer
{
   //Do something with your HUDLayer here
}

@end
lucianomarisi
  • 1,552
  • 11
  • 24
  • This way doesn't even seem to hit the method inside my HudLayer when I wrote: [hud updateKills:@"Monsters Slayed:1"] for example, hud referring to the [Hud Node] defined in the CCScene, also tried changing that to a new allocation of a HudLayer and that didn't seem to hit the method inside the Hud Node layer either (wrote this where you have the //Do somehing with your HUDLayer – user3727511 Jun 13 '14 at 18:19
0

There are many ways you can do this. But for the sake of simplicity the easiest way you can do this is via notifications. For example in the hud add:

@implementation HudLayer

- (void)onEnter
{
    [super onEnter];

    NSNotificationCenter* notiCenter = [NSNotificationCenter defaultCenter];

    [notiCenter addObserver:self
                   selector:@selector(onUpdateMonsterText:)
                       name:@"HudLayerUpdateMonsterTextNotification"
                     object:nil];
}


- (void)onExit
{
    [super onExit];

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)onUpdateMonsterText:(NSNotification *)notification
{
    NSDictionary* userInfo = notification.userInfo;

    if (userInfo)
    {
        NSString* text = userInfo[@"text"];

        if (text)
        {
            [self updateMonstersSlayed:text];
        }
        else
        {
            CCLOG(@"Monster hud text param is invalid!.");
        }
    }
    else
    {
        CCLOG(@"Monster hud user info invalid!");
    }
}

@end

Then anywhere in your application where you want to update text you can just post the notification. Using your physics collision began example:

- (BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair
    *emphasized text*collisionPlayer:(CCNode *)user collisionMonster:(CCNode *)monster
{
    if (holdingWeapon)
    {
        NSDictionary* userInfo = @{@"text" : @"Monsters Killed: 1"};
        NSString* notiName = @"HudLayerUpdateMonsterTextNotification";

        [[NSNotificationCenter defaultCenter] postNotificationName:notiName
            object:self userInfo:userInfo];

    }
}

Hope this helps.

Allen S
  • 1,042
  • 2
  • 9
  • 14