美文网首页
cocos2dx3.17.2横竖屏切换

cocos2dx3.17.2横竖屏切换

作者: 咸鱼而已 | 来源:发表于2021-01-31 16:52 被阅读0次

    在游戏中需要横竖屏切换,常用大厅到子游戏

    IOS

    1. 修改Application.mm, 首先定义变量
    static RootViewController* s_rv;
    UIInterfaceOrientationMask orientation = UIInterfaceOrientationMaskLandscape;
    
    1. didFinishLaunchingWithOptions中对s_rv进行赋值操作
     s_rv = _viewController;
    
    1. 增加下面两个方法
    - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
        return orientation;
    }
    
    + (void)setOrientation:(NSDictionary*)dic {
        NSString* dir = [dic valueForKey:@"orientation"];
        auto glview = cocos2d::Director::getInstance()->getOpenGLView();
        float scaleFactor = CC_CONTENT_SCALE_FACTOR();
        if ([dir isEqualToString:@"V"]) {
            orientation = UIInterfaceOrientationMaskPortrait;
            [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
            
            NSInteger w =s_rv.view.frame.size.width;
            NSInteger h =s_rv.view.frame.size.height;
            glview->setFrameSize(w*3, h*3);  //这一步必须设置,glview 的大小为RootViewController
        } else {
            orientation = UIInterfaceOrientationMaskLandscape;
            [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
            NSInteger w =s_rv.view.frame.size.width;
            NSInteger h =s_rv.view.frame.size.height;
            glview->setFrameSize(w*3, h*3); //这一步必须设置,glview 的大小为RootViewController
        }
    }
    
    1. 到这里ios的设置基本上完成了,可以在Lua里调用了。最后面贴lua代码

    Android

    1. 修改 AppActivity , 首先定义变量
      public static AppActivity instance = null;
      并在onCreate中初始化变量为instance = this

    2. 添加静态方法

    public static void changeOrientationH(boolean val){
        if (val == true){
            instance.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            Log.d(LOG_TAG, "切换横屏");
        }
        else{
            instance.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            Log.d(LOG_TAG, "切换竖屏");
        }
    }
    
    1. 修改CCApplication-android.cpp, 修改这个方法applicationScreenSizeChanged
    void Application::applicationScreenSizeChanged(int newWidth, int newHeight) {
        CCLOG(" android applicationScreenSizeChanged %d %d", newWidth, newHeight);
        cocos2d::GLView *cocosView = cocos2d::Director::getInstance()->getOpenGLView();
        cocosView->setFrameSize(newWidth, newHeight);
    }
    
    1. 修改Cocos2dxGLSurfaceView.java, 当朝向改变的时候,同步修改mCocos2dxRenderer
     @Override
    protected void onSizeChanged(final int pNewSurfaceWidth, final int pNewSurfaceHeight, final int pOldSurfaceWidth, final int pOldSurfaceHeight) {
        if(!this.isInEditMode()) {
            if(pNewSurfaceWidth < pNewSurfaceHeight)
                this.mCocos2dxRenderer.setScreenWidthAndHeight(pNewSurfaceHeight, pNewSurfaceWidth);
            else
                this.mCocos2dxRenderer.setScreenWidthAndHeight(pNewSurfaceWidth, pNewSurfaceHeight);
        }
    }
    

    脚本调用, 注意Lua调用oc方法的时候传参数必须是字典的形式传递

    local function changeScreenOrientationH(val)
        if device.platform == 'android' then
            local luabridge = require('cocos.cocos2d.luaj')
            local METHOD_NAME = 'changeOrientationH'
            local JAVA_CLASS_NAME = 'org/cocos2dx/lua/AppActivity'
    
            local suc, version = luabridge.callStaticMethod(JAVA_CLASS_NAME, METHOD_NAME, {val}, '(Z)V')
        elseif device.platform == 'ios' then
            local luabridge = require('cocos.cocos2d.luaoc')
            local AppController = "AppController"
            local orientation = val and "H" or "V"
            local suc, id = luabridge.callStaticMethod(AppController, "setOrientation", {orientation = orientation})
            if suc == true and id ~= "" then 
                return id
            end
        end
    end
    

    相关文章

      网友评论

          本文标题:cocos2dx3.17.2横竖屏切换

          本文链接:https://www.haomeiwen.com/subject/lsyrtltx.html