Skip to content

Instantly share code, notes, and snippets.

@coryalder
Created January 11, 2010 00:30
Show Gist options
  • Save coryalder/273888 to your computer and use it in GitHub Desktop.
Save coryalder/273888 to your computer and use it in GitHub Desktop.
// extends http://johnehartzog.com/2009/10/easy-to-create-buttons-with-cocos2d/
// give you a button that toggles between two images
// uses cocos2d 0.9, so all the classes are called CCMenuItem instead of MenuItem.
// other than that it should work with older versions of cocos2d
+ (id)buttonWithImage:(NSString*)file
andImage:(NSString *)otherFile
atPosition:(CGPoint)position
target:(id)target
selector:(SEL)selector
{
CCButtonItem *button1 = [CCButtonItem buttonWithImage:file target:target selector:selector];
CCButtonItem *button2 = [CCButtonItem buttonWithImage:otherFile target:target selector:selector];
CCMenuItemToggle *toggle = [CCMenuItemToggle itemWithTarget:target selector:selector items:button1,button2,nil];
CCMenu *menu = [CCMenu menuWithItems:toggle, nil];
menu.position = position;
return menu;
}
// this is the selector passed to buttonWithImage:
-(void)pauseToggle:(id)sender {
CCMenuItemToggle *toggle = (CCMenuItemToggle *)sender;
if ([toggle selectedIndex] == 0) {
[self resume:sender];
} else {
[self pause:sender];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment