游戏不能旋转PortraitUpsideDown

时间:2015-02-12 11:08:01

标签: ios cocos2d-iphone

方向显示在info.plist文件的“支持界面方向”中,但转动后仍然不会旋转。

我希望制作肖像& Portraitupsidedown

这是来自RootViewController.m

   - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

        //
        // There are 2 ways to support auto-rotation:
        //  - The OpenGL / cocos2d way
        //     - Faster, but doesn't rotate the UIKit objects
        //  - The ViewController way
        //    - A bit slower, but the UiKit objects are placed in the right place
        //

    #if GAME_AUTOROTATION==kGameAutorotationNone
        //
        // EAGLView won't be autorotated.
        // Since this method should return YES in at least 1 orientation, 
        // we return YES only in the Portrait orientation
        //

    #elif GAME_AUTOROTATION==kGameAutorotationCCDirector
        //
        // EAGLView will be rotated by cocos2d
        //
        // Sample: Autorotate only in landscape mode
        //
        if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) {
            [[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeRight];
        } else if( interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
            [[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeLeft];
        }

        // Since this method should return YES in at least 1 orientation,
        // we return YES only in the Portrait orientation
        return ( interfaceOrientation == UIInterfaceOrientationPortrait );

    #elif GAME_AUTOROTATION == kGameAutorotationUIViewController
        //
        // EAGLView will be rotated by the UIViewController
        //
        // Sample: Autorotate only in landscpe mode
        //
        // return YES for the supported orientations

        return ( UIInterfaceOrientationIsPortrait(interfaceOrientation));

    #else
    #error Unknown value in GAME_AUTOROTATION

    #endif // GAME_AUTOROTATION


        // Shold not happen
        return NO;
    }

    //
    // This callback only will be called when GAME_AUTOROTATION == kGameAutorotationUIViewController
    //
    #if GAME_AUTOROTATION == kGameAutorotationUIViewController
    -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        //
        // Assuming that the main window has the size of the screen
        // BUG: This won't work if the EAGLView is not fullscreen
        ///
        CGRect screenRect = [[UIScreen mainScreen] bounds];
        CGRect rect = CGRectZero;

        if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
            rect = screenRect;


        else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
            rect.size = CGSizeMake( screenRect.size.height, screenRect.size.width );

        CCDirector *director = [CCDirector sharedDirector];
        EAGLView *glView = [director openGLView];
        float contentScaleFactor = [director contentScaleFactor];

        if( contentScaleFactor != 1 ) {
            rect.size.width *= contentScaleFactor;
            rect.size.height *= contentScaleFactor;
        }
        glView.frame = rect;
    }
    #endif // GAME_AUTOROTATION == kGameAutorotationUIViewController


From the AppDelegate.m file I also have;

    // Init the window
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

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

    // Init the View Controller
    viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
    viewController.wantsFullScreenLayout = YES;

    //
    // Create the EAGLView manually
    //  1. Create a RGB565 format. Alternative: RGBA8
    //  2. depth format of 0 bit. Use 16 or 24 bit for 3d effects, like CCPageTurnTransition
    //
    //
    EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
                                   pixelFormat:kEAGLColorFormatRGB565   // kEAGLColorFormatRGBA8
                                   depthFormat:0                        // GL_DEPTH_COMPONENT16_OES
                        ];

    // attach the openglView to the director
    [director setOpenGLView:glView];

//  // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
    if( ! [director enableRetinaDisplay:YES] )
        CCLOG(@"Retina Display Not supported");

    //
    // VERY IMPORTANT:
    // If the rotation is going to be controlled by a UIViewController
    // then the device orientation should be "Portrait".
    //
    // IMPORTANT:
    // By default, this template only supports Landscape orientations.
    // Edit the RootViewController.m file to edit the supported orientations.
    //

    [director setDeviceOrientation:kCCDeviceOrientationPortrait];
    [director setAnimationInterval:1.0/60];
    [director setDisplayFPS:NO];


    // make the OpenGLView a child of the view controller
    [viewController setView:glView];

    // make the View Controller a child of the main window
    [window addSubview: viewController.view];

    [window makeKeyAndVisible];

我有其他游戏,使用Cocos2d,我已设法旋转,我用那些交叉检查设置,它们是相同的,所以我无法弄清楚它为什么不旋转?

编辑 -

这可能是一个有关此事的明确信息......

Application windows are expected to have a root view controller at the end of application launch

2 个答案:

答案 0 :(得分:0)

尝试添加rootViewController

[window setRootViewController: viewController];

答案 1 :(得分:0)

在rootview控制器中,注释掉

CCDirector *director = [CCDirector sharedDirector];
EAGLView *glView = [director openGLView];
float contentScaleFactor = [director contentScaleFactor];

if( contentScaleFactor != 1 ) {
    rect.size.width *= contentScaleFactor;
    rect.size.height *= contentScaleFactor;
}
glView.frame = rect;