Macarse
Macarse

Reputation: 93173

Using CCMenuItemImage to create single buttons in the hud layer

I just started a game using cocos2d-iphone and I am trying to add a pause botton in my hud. My hud will have:

I have been reading Ray Wenderlich's tutorials and he mentions for buttons CCMenuItemImage should be used. I first did:

CCMenuItemImage *pauseButton = [CCMenuItemImage itemFromNormalImage:@"hud_pause_bt.png" selectedImage:@"hud_pause_bt.png" target:self selector:@selector(pauseAction:)];
pauseButton.position = ccp(winSize.width - pauseButton.rect.size.width/1.8, winSize.height - pauseButton.rect.size.height/1.8);

[self addChild:pauseButton];

but I was never reaching the pauseAction method. Afterwards I tried adding the CCMenuItemImage to a CCMenu with the following code:

CGSize winSize = [CCDirector sharedDirector].winSize;

CCMenuItemImage *pauseButton = [CCMenuItemImage itemFromNormalImage:@"hud_pause_bt.png" selectedImage:@"hud_pause_bt.png" target:self selector:@selector(pauseAction:)];

pauseButton.position = ccp(winSize.width - pauseButton.rect.size.width/1.8, winSize.height - pauseButton.rect.size.height/1.8);

CCMenu *pauseMenu = [CCMenu menuWithItems:pauseButton, nil];
pauseMenu.position = CGPointZero;
[self addChild:pauseMenu];

In this case the touches work but it feels wrong to create a CCMenu just to make my CCMenuItemImage clickable.

What do you think?

Upvotes: 1

Views: 2366

Answers (1)

James Webster
James Webster

Reputation: 32076

The way you have done it is correct. That is the way CCMenu and CCMenuItems are designed to be used.

Consider just using a CCStandardTouchDelegate combined with CGRectContainsPoint if you don't like that method.

Upvotes: 3

Related Questions