游戏不旋转 PortraitUpsideDown

Posted

技术标签:

【中文标题】游戏不旋转 PortraitUpsideDown【英文标题】:Game not rotating PortraitUpsideDown 【发布时间】:2015-02-12 11:08:00 【问题描述】:

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

我希望制作 Portrait & 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

【问题讨论】:

你试过追踪它吗?设置断点以及发生了什么。 我不确定在哪里放置断点? 【参考方案1】:

尝试添加rootViewController

[window setRootViewController: viewController];

【讨论】:

嗨@gururaj.T,效果更好,它现在可以旋转,但一切都在屏幕外,即它旋转并出现黑屏(所以一切都在屏幕外) 好的解决方案是将代码移植到 Cocos2d 2.x 或 3.x【参考方案2】:

在你的 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;

【讨论】:

以上是关于游戏不旋转 PortraitUpsideDown的主要内容,如果未能解决你的问题,请参考以下文章

记忆游戏卡旋转不正确

旋转游戏

用JavaScript编码2D游戏物理,不平坦的地面和旋转

Unity3D视图中心 ( 视图中心概念 | 围绕游戏物体旋转 | 添加游戏物体到游戏场景的位置 )

我通过倾斜手机来控制游戏对象,并且需要在游戏开始时手机旋转时保持平衡

我的手机开启了屏幕旋转但是玩王者荣耀的时候还是不能旋转屏幕其他游戏也试过都不行