I wrote some classes to help me do this easily. You can check it out at http://johnehartzog.com/2009/10/easy-to-create-buttons-with-cocos2d/.
Here is the code if you just want it here.
Usage is simple, do this
[self addChild:[Button buttonWithText:@"back" atPosition:ccp(80, 50) target:self selector:@selector(back:)]];
[self addChild:[Button buttonWithImage:@"openFeint.png" atPosition:ccp(400, 50) target:self selector:@selector(openOpenFeint:)]];
The class uses two images, button.png is the normal image behind the text and the button_p.png is the image that is shown when you are touching the button.
Here is the class:
//
// Button.h
// StickWars - Siege
//
// Created by EricH on 8/3/09.
//
@interface Button : Menu {
}
+ (id)buttonWithText:(NSString*)text atPosition:(CGPoint)position target:(id)target selector:(SEL)selector;
+ (id)buttonWithImage:(NSString*)file atPosition:(CGPoint)position target:(id)target selector:(SEL)selector;
@end
@interface ButtonItem : MenuItem {
Sprite *back;
Sprite *backPressed;
}
+ (id)buttonWithText:(NSString*)text target:(id)target selector:(SEL)selector;
+ (id)buttonWithImage:(NSString*)file target:(id)target selector:(SEL)selector;
- (id)initWithText:(NSString*)text target:(id)target selector:(SEL)selector;
- (id)initWithImage:(NSString*)file target:(id)target selector:(SEL)selector;
@end
//
// Button.m
// StickWars - Siege
//
// Created by EricH on 8/3/09.
//
#import "Button.h"
@implementation Button
+ (id)buttonWithText:(NSString*)text atPosition:(CGPoint)position target:(id)target selector:(SEL)selector {
Menu *menu = [Menu menuWithItems:[ButtonItem buttonWithText:text target:target selector:selector], nil];
menu.position = position;
return menu;
}
+ (id)buttonWithImage:(NSString*)file atPosition:(CGPoint)position target:(id)target selector:(SEL)selector {
Menu *menu = [Menu menuWithItems:[ButtonItem buttonWithImage:file target:target selector:selector], nil];
menu.position = position;
return menu;
}
@end
@implementation ButtonItem
+ (id)buttonWithText:(NSString*)text target:(id)target selector:(SEL)selector {
return [[[self alloc] initWithText:text target:target selector:selector] autorelease];
}
+ (id)buttonWithImage:(NSString*)file target:(id)target selector:(SEL)selector {
return [[[self alloc] initWithImage:file target:target selector:selector] autorelease];
}
- (id)initWithText:(NSString*)text target:(id)target selector:(SEL)selector {
if(self = [super initWithTarget:target selector:selector]) {
back = [[Sprite spriteWithFile:@"button.png"] retain];
back.anchorPoint = ccp(0,0);
backPressed = [[Sprite spriteWithFile:@"button_p.png"] retain];
backPressed.anchorPoint = ccp(0,0);
[self addChild:back];
self.contentSize = back.contentSize;
Label* textLabel = [Label labelWithString:text fontName:@"take_out_the_garbage" fontSize:22];
textLabel.position = ccp(self.contentSize.width / 2, self.contentSize.height / 2);
textLabel.anchorPoint = ccp(0.5, 0.3);
[self addChild:textLabel z:1];
}
return self;
}
- (id)initWithImage:(NSString*)file target:(id)target selector:(SEL)selector {
if(self = [super initWithTarget:target selector:selector]) {
back = [[Sprite spriteWithFile:@"button.png"] retain];
back.anchorPoint = ccp(0,0);
backPressed = [[Sprite spriteWithFile:@"button_p.png"] retain];
backPressed.anchorPoint = ccp(0,0);
[self addChild:back];
self.contentSize = back.contentSize;
Sprite* image = [Sprite spriteWithFile:file];
[self addChild:image z:1];
image.position = ccp(self.contentSize.width / 2, self.contentSize.height / 2);
}
return self;
}
-(void) selected {
[self removeChild:back cleanup:NO];
[self addChild:backPressed];
[super selected];
}
-(void) unselected {
[self removeChild:backPressed cleanup:NO];
[self addChild:back];
[super unselected];
}
// this prevents double taps
- (void)activate {
[super activate];
[self setIsEnabled:NO];
[self schedule:@selector(resetButton:) interval:0.5];
}
- (void)resetButton:(ccTime)dt {
[self unschedule:@selector(resetButton:)];
[self setIsEnabled:YES];
}
- (void)dealloc {
[back release];
[backPressed release];
[super dealloc];
}
@end