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:
- Time left
- HP bar
- Pause button
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:开发者_如何学CpauseButton, 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?
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.
精彩评论