0

所以我一直在尝试为我开发的游戏创建菜单。我正在使用 Cocos2d 并且游戏设置为纵向。我写的 menu.m 文件是这样的。

// Import the interfaces
#import "Menu.h"
#import "BankerInfo.h"

// HelloWorldLayer implementation
@implementation MenuLayer

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

// 'layer' is an autorelease object.
MenuLayer *layer = [MenuLayer node];

// add layer as a child to scene
[scene addChild: layer];

// return the scene
return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {

    // ask director the the window size
    CGSize winSize = [[CCDirector sharedDirector] winSize];

    CCSprite *menubackground = [CCSprite spriteWithFile:@"Ninja Menu Background.png"];
    menubackground.position = ccp(winSize.width/2, winSize.height/2);
    [self addChild:menubackground z:-1];


    CCLabelTTF *title = [CCLabelTTF labelWithString:@"Catch It If You Can!"             fontName:@"Zapfino" fontSize:22];
    title.position =  ccp(160, 420);
    [self addChild: title];

    CCLayer *menuLayer = [[CCLayer alloc] init];
    [self addChild:menuLayer];

    CCMenuItem *Bankerbutton = [CCMenuItemImage
                               itemFromNormalImage:@"Bankernotpressed.png"
                               selectedImage:@"Bankerpressed.png"
                               target:self
                               selector:@selector(startBanker:)];
    //Bankerbutton.position = ccp(100, 175);

    CCMenuItem *Babybutton = [CCMenuItemImage
                                 itemFromNormalImage:@"Babynotpressed.png"
                                 selectedImage:@"Babypressed.png"
                                 target:self
                                 selector:@selector(startBaby:)];
    //Babybutton.position = ccp(100, 75);
    CCMenuItem *Mommaduckbutton = [CCMenuItemImage
                              itemFromNormalImage:@"Mommaducknotpressed.png"
                              selectedImage:@"Mommaduckpressed.png"
                              target:self
                              selector:@selector(startMommaduck:)];
    //Mommaduckbutton.position = ccp(350, 175);
    CCMenuItem *Baseballbutton = [CCMenuItemImage
                                itemFromNormalImage:@"Baseballnotpressed.png"
                                selectedImage:@"Baseballpressed.png"
                                target:self
                                selector:@selector(startArcher:)];
    //Baseballbutton.position = ccp(350, 75);
    CCMenuItem *Newtonbutton = [CCMenuItemImage
                                  itemFromNormalImage:@"Newtonnotpressed.png"
                                  selectedImage:@"Newtonpressed.png"
                                  target:self
                                  selector:@selector(startArcher:)];
    //Newtonbutton.position = ccp(350, 75); 

    CCMenu *menu = [CCMenu menuWithItems: Bankerbutton, Babybutton, Mommaduckbutton,         Baseballbutton, Newtonbutton, nil];
    menu.position = ccp(winSize.width/2, (winSize.height/2)-30);
    [menu alignItemsInRows:
     [NSNumber numberWithInt:2], [NSNumber numberWithInt:2], nil];
    [menuLayer addChild: menu];

}
return self;
}

- (void) startBanker: (id) sender
{
[[CCDirector sharedDirector] replaceScene:[BankerInfoLayer scene]];
}
/*- (void) startBaby: (id) sender
{
[[CCDirector sharedDirector] replaceScene:[Backgroundinfo_Soldier scene]];
}
- (void) startMommaduck: (id) sender
{
[[CCDirector sharedDirector] replaceScene:[Backgroundinfo_Mage scene]];
}
- (void) startBaseball: (id) sender
{
[[CCDirector sharedDirector] replaceScene:[Backgroundinfo_Archer scene]];
}
- (void) startNewton: (id) sender
{
[[CCDirector sharedDirector] replaceScene:[Backgroundinfo_Archer scene]];
}
*/
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)

// don't forget to call "super dealloc"
[super dealloc];
}
@end

我得到的错误看起来像这样。

Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '+[NSInvocation invocationWithMethodSignature:]: 
method signature argument cannot be nil'

有任何想法吗?

4

1 回答 1

0

该错误指出,这是由于对不存在的方法签名的错误调用而发生的。如果你提供菜单创建时使用的功能,它仍然会崩溃吗?

第 1 步:确保棒球按钮和牛顿按钮指向正确的选择器,而不仅仅是不存在的“startArcher:”。

CCMenuItem *Baseballbutton = [CCMenuItemImage
                            itemFromNormalImage:@"Baseballnotpressed.png"
                            selectedImage:@"Baseballpressed.png"
                            target:self
                            selector:@selector(startBaseball:)]; // CHANGED HERE
//Baseballbutton.position = ccp(350, 75);
CCMenuItem *Newtonbutton = [CCMenuItemImage
                              itemFromNormalImage:@"Newtonnotpressed.png"
                              selectedImage:@"Newtonpressed.png"
                              target:self
                              selector:@selector(startNewton:)]; // CHANGED HERE

第 2 步:取消注释函数,以便您实际上拥有有效的选择器

- (void) startBanker: (id) sender
{
    [[CCDirector sharedDirector] replaceScene:[BankerInfoLayer scene]];
}
- (void) startBaby: (id) sender
{
    [[CCDirector sharedDirector] replaceScene:[Backgroundinfo_Soldier scene]];
}
- (void) startMommaduck: (id) sender
{
    [[CCDirector sharedDirector] replaceScene:[Backgroundinfo_Mage scene]];
}
- (void) startBaseball: (id) sender
{
    [[CCDirector sharedDirector] replaceScene:[Backgroundinfo_Archer scene]];
}
- (void) startNewton: (id) sender
{
    [[CCDirector sharedDirector] replaceScene:[Backgroundinfo_Archer scene]];
}
于 2012-07-19T00:49:30.963 回答