2011-12-26 2 views
2

나는 cocos2d 공식 사이트에서 지침을 따랐습니다. 하나의 매개 변수를 사용하여 선택기를 전달할 때 메뉴를 만들기위한 일부 항목을 만들려고합니다. 각 항목에 대해 다른 선택기를 전달합니다. 나는 여기에 문제가 있다고 생각하지만, 왜 여기에 문제가 있는지를 정말로 보지 못한다. 내 헤더 파일은 같습니다NSInvocation invocationWithMethodSignature, 메서드 서명 인수는 nil이 될 수 없습니다.

// When you import this file, you import all the cocos2d classes 
#import "cocos2d.h" 
#import "CCTouchDispatcher.h" 

// HelloWorldLayer 
@interface HelloWorldLayer : CCLayer { 

    CCSprite *first; 
    CCSprite *second; 
} 

// returns a CCScene that contains the HelloWorldLayer as the only child 
+(CCScene *) scene; 
- (void) setUpMenus; 
- (void) doSomethingOne: (CCMenuItem *) menuItem; 
- (void) doSomethingTwo: (CCMenuItem *) menuItem; 
- (void) doSomethingThree: (CCMenuItem *) menuItem; 

@end 

실행 파일 : 당신은 세 가지 메뉴 항목을 생성 통화에서 동일한 오타가있어

// Import the interfaces 
#import "HelloWorldLayer.h" 

// HelloWorldLayer implementation 
@implementation HelloWorldLayer 

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

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

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

    // return the scene 
    return scene; 
} 

- (void) doSomethingOne: (CCMenuItem *) menuItem 
{ 
    NSLog(@"The first menu was called"); 
} 
- (void) doSomethingTwo: (CCMenuItem *) menuItem 
{ 
    NSLog(@"The second menu was called"); 
} 
- (void) doSomethingThree: (CCMenuItem *) menuItem 
{ 
    NSLog(@"The third menu was called"); 
} 


// 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])) { 

     first = [CCSprite spriteWithFile:@"seeker.png"]; 
     first.position = ccp(100, 100); 

     [self addChild:first]; 

     second = [CCSprite spriteWithFile:@"Icon.png"]; 
     second.position = ccp(50, 50); 

     [self addChild:second]; 

     [self schedule:@selector(nextFrame:)]; 

     [self setUpMenus]; 

     self.isTouchEnabled = YES; 
    } 

    return self; 
} 


- (void) registerWithTouchDispatcher { 

    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; 
} 

- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 

    return YES; 
} 

- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { 

    CGPoint location = [self convertTouchToNodeSpace: touch]; 

    [second stopAllActions]; 

    [second runAction: [CCMoveTo actionWithDuration:1 position:location]]; 

} 

- (void) nextFrame:(ccTime)dt { 

    first.position = ccp(first.position.x + 100*dt, first.position.y); 

    if (first.position.x > 480+32) { 

     first.position = ccp(-32, first.position.y); 
    } 
} 

- (void) setUpMenus { 

    CCMenuItemImage *menuItem1 = [CCMenuItemImage itemFromNormalImage:@"myfirstbutton.png" 
                 selectedImage:@"myfirstbutton_selected.png" 
                   target:self 
                  selector:@selector(doSomenthingOne:)]; 

    CCMenuItemImage *menuItem2 = [CCMenuItemImage itemFromNormalImage:@"mysecondbutton.png" 
                 selectedImage:@"mysecondbutton_selected.png" 
                   target:self 
                  selector:@selector(doSomenthingTwo:)]; 
    CCMenuItemImage *menuItem3 = [CCMenuItemImage itemFromNormalImage:@"mythirdbutton.png" 
                 selectedImage:@"mythirdbutton_selected.png" 
                   target:self selector:@selector(doSomenthingThree:)]; 

    CCMenu *myMenu = [CCMenu menuWithItems:menuItem1,menuItem2,menuItem3, nil]; 

    [myMenu alignItemsVertically]; 

    [self addChild:myMenu]; 

} 



// 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 

답변

1

.

CCMenuItemImage *menuItem1 = [... selector:@selector(doSomenthingOne:)]; 
CCMenuItemImage *menuItem2 = [... selector:@selector(doSomenthingTwo:)]; 
CCMenuItemImage *menuItem3 = [... selector:@selector(doSomenthingThree:)]; 

을하지만 방법의 실제 이름은 doSomethingOne:, doSomethingTwo:doSomethingThree: 위치 : (중간에 가짜 N주의)가 사용해야하는 선택이 doSomenthing...라고 메뉴 항목을 말하는 것입니다.

정확한 오류 메시지의 원인은 나중에 메뉴 항목에서 해당 선택기를 수행해야 할 때 클래스가 제공 한 선택기에 대한 메서드 서명을 클래스에 요청할 것입니다. 항목에 잘못된 셀렉터를 지정했기 때문에 클래스는 서명을 알지 못하며 nil을 반환합니다. 메뉴 항목은 NSInvocation 오브젝트를 구성하려고 시도하지만 해당 조치를 수행 할 수 없습니다. 호출은 nil 서명으로 작성 될 수 없으므로 실패합니다.

오타를 수정하면 정상적으로 작동합니다.

+0

나는 그것을 관찰하지 않았다. thx –