I'm trying to create a menu system that calls a method depending on what is pressed. The problem is when I add a target and selector to the CCMenuitems.It crashes with a sgabrt error. I know the problem is something to do with the target, but what should it be? Here is my .h and .m code
#import "cocos2d.h"
// splashMenuLayer
@interface splashMenuLayer : CCLayer
{
BOOL menuButtonsShowing;
CCLabelTTF * splashLabel;
CCMenuItemFont * puzzleMenuItem;
CCMenuItemFont * raceMenuItem;
CCMenuItemFont * leaderboardMenuItem;
CCMenu * mainMenu;
}
// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;
-(BOOL) ccTouchBegan:(UITouch *)touch wi开发者_如何转开发thEvent:(UIEvent *)event;
-(void) ccTouchEnded:(NSSet *)touches withEvent:(UIEvent *)event;
-(void) deleteLabel :(id)sender;
-(void) puzzleMode:(id)sender;
-(void) raceMode:(id)sender;
-(void) Leaderboard:(id)sender;
@property BOOL menuButtonsShowing;
@property (nonatomic, retain) CCLabelTTF* splashLabel;
@property (nonatomic, retain) CCMenuItem* puzzleMenuItem;
@property (nonatomic, retain) CCMenuItem* raceMenuItem;
@property (nonatomic, retain) CCMenuItem* leaderboardMenuItem;
@property (nonatomic, retain) CCMenu* mainMenu;
@end
and finally
-(void) ccTouchEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if (menuButtonsShowing == NO) {
id action = [CCSequence actions:[CCFadeOut actionWithDuration:0.5],[CCCallFunc actionWithTarget:self selector:@selector(deleteLabel:)], nil];
[splashLabel runAction:action];
//create the menu items and all the target/selector
puzzleMenuItem = [CCMenuItemFont itemFromString:@"Puzzle Mode" target:self selector:@selector(puzzleMode:)];
raceMenuItem = [CCMenuItemFont itemFromString:@"Race Mode" target:self selector:@selector(raceMode:)];
leaderboardMenuItem = [CCMenuItemFont itemFromString:@"Leaderboard" target:self selector:@selector(leaderboard:)];
//add the menu items to the menu
mainMenu = [CCMenu menuWithItems:puzzleMenuItem, raceMenuItem, leaderboardMenuItem, nil];
[mainMenu alignItemsVertically];
mainMenu.position = ccp(240, 100);
[self addChild:mainMenu];
}
}
-(void) puzzleMode:(id)sender{
NSLog(@"lol1");
}
-(void) raceMode:(id)sender{
NSLog(@"lol2");
}
-(void) Leaderboard:(id)sender{
NSLog(@"lol3");
}
correct this line with capital L
leaderboardMenuItem = [CCMenuItemFont itemFromString:@"Leaderboard" target:self selector:@selector(leaderboard:)];
to
leaderboardMenuItem = [CCMenuItemFont itemFromString:@"Leaderboard" target:self selector:@selector(Leaderboard:)];
The only thing I've been able to spot is that @selector(leaderboard:)
should be @selector(Leaderboard:)
(capitalized).
Mike
精彩评论