我正在创建一个游戏,允许设备在左侧或横向的方向,而播放器可以在暂停时改变方向.当我们需要改变游戏根据方向解释加速度计的方式时.
在iOS 5中,我使用willRotatetoInterfaceOrientation来捕获更改并更改我的变量,但是这在iOS6中已经不推荐了.我现有的代码如下所示:
if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
rect = screenRect;
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
rect.size = CGSizeMake( screenRect.size.height,screenRect.size.width );
GameEngine *engine = [GameEngine sharedEngine];
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
engine.orientation = -1;
} else {
engine.orientation = 1;
}
}
我明白替换是UIViewController类中的viewWillLayoutSubviews方法.我正在cocos2d 2.1中构建这个游戏,并且在演示项目中似乎不是一个UIViewController类,所以我不清楚如何并入它,以及代码应该如何使其工作.
解决方法
聆听设备方向更改:
[[NSNotificationCenter defaultCenter]
addobserver:self
selector:@selector(deviceOrientationDidChangeNotification:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
通知时,从UIDevice获取设备方向:
- (void)deviceOrientationDidChangeNotification:(NSNotification*)note
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
switch (orientation)
{
// etc...
}
}