从子层Cocos2d中的菜单调用父场景中的方法

时间:2012-10-21 20:38:23

标签: objective-c ios cocos2d-iphone

我在我的gamePlay场景中添加了一个图层(hudLayer),然后在该图层中添加了一个菜单,我想从中运行一个函数- (void) pauseGame { method in here }

我正试图像这样实现:

- (void) addPauseButton: (CCScene *) parent {
pauseButton = [CCMenuItemImage itemWithNormalImage:@"pause.png" selectedImage:nil target:parent selector:@selector(pauseGame:)];
pause = [CCMenu menuWithItems:pauseButton, nil];
[hudLayer addChild: pause];
if (device == @"iPad") {
    pause.position = ccp(40,40);
}
else if (device == @"iPhoneFive") {
    pause.position = ccp(290,30);
}
else {
    pause.position = ccp(290,30);
}
}

- (id)init {

if( (self=[super init])) {
    paused = FALSE;
    CGSize screenSize = [CCDirector sharedDirector].winSize;

    hudLayer = [[[CCLayer alloc] init] autorelease];
    [self addChild:hudLayer z:2]; // adds hudLayer

    gameLayer = [[[CCLayer alloc] init] autorelease];
    [self addChild:gameLayer z:1];

    pauseMenu = [[[CCLayer alloc] init] autorelease];
    [self addChild:pauseMenu z:4];

    [self setupWorld];
    [self addPauseButton:self];   //calls method to add pause button to hudLayer sending it the scene as a variable

    CGPoint offScreenPoint = ccp(screenSize.width+(screenSize.width/2), 0);

    pauseMenu.position = offScreenPoint;

    self.motionManager = [[[CMMotionManager alloc] init] autorelease];
    motionManager.deviceMotionUpdateInterval = 1.0/60.0;
    if (motionManager.isDeviceMotionAvailable) {
        [motionManager startDeviceMotionUpdates];
    }
    self.iPadBool = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;

    if (iPadBool) {
        device = @"iPad";
    }
    else if (screenSize.height > 490) {
        device = @"iPhoneFive";
    }
    else {
        device = @"iPhone";
    }
    NSLog(@"the current device is a, %@", device);
    [self schedule:@selector(update:)];

}
return self;
}

但是这会引发错误: * 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [gamePlay pauseGame:]:无法识别的选择器发送到实例0x11d310e0'根本不知道为什么: /任何想法?

1 个答案:

答案 0 :(得分:0)

选择器应为

@selector(pauseGame)

没有冒号。它不需要参数。方法应该是:

-(void) pauseGame
{
}