Handle Button in CCNode from CCScene

126 views Asked by At

I'm creating a game in cocos2d v3. In the CCScene, I add a CCNode that contains all the components for my HUD. In the CCNode, there are CCButtons added. I want to be able to handle the touch of these buttons in my CCScene. Is that possible? And if so, how is it done elegantly?

Code that I tested this morning and is working:

CCNode header file (Header class):

@property (nonatomic, retain) CCButton *goldButton;

I set up the basics of the gold button in the implementation file of the CCNode (positioning, sprite frame, etc).

CCScene implementation file (PlayScene class):

-(void) setup {
    _header = [[Header alloc] init];
    [_header.goldButton setTarget:self selector:@selector(goldButtonTapped)];
}

Seems a little weird setting the target of the button inside the scene, but it works. I am wondering of the 'best practice', if you will, for this situation.

0

There are 0 answers