首先,以下是UIViewController子类的实现代码,我们加入了HellerWord,它是CCLayer的一个子类。
-(void)loadView{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveTestNotification:)
name:@"TestNotification"
object:nil];
CC_DIRECTOR_INIT();
// Try to use CADisplayLink director
// if it fails (SDK < 3.1) use the default director
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeDefault];
CCDirector *director = [CCDirector sharedDirector];
self.wantsFullScreenLayout = YES;
EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGB565
depthFormat:0];
// attach the openglView to the director
[director setOpenGLView:glView];
[self setView:glView];
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
#else
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
#endif
[director setAnimationInterval:1.0/60];
// Run the intro Scene
CCScene *scene = [HelloWorldLayer scene];
[[CCDirector sharedDirector] runWithScene:scene];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveTestNotification:)
name:@"TestNotification"
object:nil];
CC_DIRECTOR_INIT();
// Try to use CADisplayLink director
// if it fails (SDK < 3.1) use the default director
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeDefault];
CCDirector *director = [CCDirector sharedDirector];
self.wantsFullScreenLayout = YES;
EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGB565
depthFormat:0];
// attach the openglView to the director
[director setOpenGLView:glView];
[self setView:glView];
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
#else
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
#endif
[director setAnimationInterval:1.0/60];
// Run the intro Scene
CCScene *scene = [HelloWorldLayer scene];
[[CCDirector sharedDirector] runWithScene:scene];
}
- (void) receiveTestNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"TestNotification"])
NSLog (@"Successfully received the test notification!");
}
以上码的第一句是:[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveTestNotification:)
name:@"TestNotification"
object:nil];
其作用是用于接收来自CCLayer子类HollerWorld中发回来的事件。相应的响应方法为以上代码中的
- (void) receiveTestNotification:(NSNotification *) notification。
那么在HollerWorld中就要有类似以下的事件发送方法
- (void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *) event{
NSLog(@"CCLayer touches has began");
[[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" object:self];
}
NSLog(@"CCLayer touches has began");
[[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" object:self];
}
浙公网安备 33010602011771号