0
我增加了層(hudLayer)到我的遊戲場景,然後添加一個菜單,從中我想運行一個功能層- (void) pauseGame { method in here }
調用從菜單中父場景的方法在其子層的Cocos2D
我想實現這個像這樣:
- (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」,原因是:「 - [遊戲pauseGame:]:無法識別的選擇發送到實例0x11d310e0'不知道爲什麼:/任何想法的?
dangnabbit這麼簡單! :P – simonthumper
是的,冒號表示參數,所以對於您的示例,它無法找到具有一個參數的選擇器。 – chrs