cocos如何在游戏中动态设置横竖屏

https://blog.csdn.net/oJianYue12/article/details/80927700

 

最近在做一个棋牌项目,有个需求,是当点击某个按钮的时候,游戏从横屏转到竖屏,再次点击的时候,游戏从竖屏再转到横屏。之前写过一个更改项目横竖屏的博客,但是那个只是在确定项目屏幕方向的时候设置。这次的更改,与上次不同。进入正题:

项目是使用LUA,所以需要对应的在android和ios双平台分别做切换处理,在通过lua,调用不同的平台处理。

一、android平台
    1、首先我们需要在android的AppActivity.java添加如下代码

    //设置手机的旋转方向1:横屏,2:竖屏,3根据用户朝向
    public static int setOrientation(int orientation){
        if(orientation == 1 ) {
            m_activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            m_activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        }else if (orientation == 2 ){
            m_activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//            m_activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        }else if (orientation == 3 ) {
            m_activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
            m_activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
        }
        return 0;
    }
1)当orientation为1的时候,第一个设置为设置横屏,不随重力感应器切换方向。第二个为根据手机重力感应,设置横屏的上下方向

2)当orientation为2的时候,第一个设置为设置竖屏,不随重力感应器切换方向。第二个为根据手机重力感应,设置竖屏的上下方向

设置完之后,android处理方法添加完毕,我们再去IOS里面添加对应的IOS代码

二、IOS平台
首先我们添加一个转换方法,将lua即将传过来的方向的值,转化为ios里面对应的Boolean值

int GameIOS::setOrientation(int screen)
{
    if(screen == 1)
    {
        [AppController setOrientation:false];
    }
    else if(screen == 2)
    {
        [AppController setOrientation:true];
    }
    return 0;
}
然后,我们需要在AppContriller.mm里面,实现这个setOrientation方法,代码如下

static bool g_bIsPortrait = false;

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
{
    if (g_bIsPortrait == false) {
        //横屏
        return UIInterfaceOrientationMaskLandscape;
        
    }else{
        //竖屏
        return UIInterfaceOrientationMaskPortrait;
        
    }
}

+(void)setOrientation:(bool)bIsPortrait{
    
    if(g_bIsPortrait == bIsPortrait){
        return ;
    }
    g_bIsPortrait = bIsPortrait;
    UIInterfaceOrientation interfaceOrientation = UIInterfaceOrientationUnknown;
    if(bIsPortrait){
        interfaceOrientation =UIInterfaceOrientationPortrait;
    }
    else{
        interfaceOrientation =UIInterfaceOrientationLandscapeRight;
    }
    //NSLog(@"%d,setOrientation",bIsPortrait);
    NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
    
    [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
    
    NSNumber *orientationTarget = [NSNumber numberWithInt:interfaceOrientation];
    
    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
    /*
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector  = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = orientation;
        // 从2开始是因为0 1 两个参数已经被selector和target占用
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
     */
    /*
     g_bIsPortrait = bIsPortrait;
     UIInterfaceOrientation interfaceOrientation = UIInterfaceOrientationUnknown;
     if(bIsPortrait){
     interfaceOrientation =UIInterfaceOrientationPortrait;
     }
     else{
     interfaceOrientation =UIInterfaceOrientationLandscapeLeft;
     }
     
     NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
     
     [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
     
     NSNumber *orientationTarget = [NSNumber numberWithInt:interfaceOrientation];
     
     [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
     */
}
OK,到这里,我们android和IOS端的实现都已经实现,接下来要做的,就是在lua里面去调用这些实现,有两种方式,一种使用lua直接调用,一种是使用C++分别调用android和IOS,我们这里使用第一种

三、使用C++调用android和IOS

我们只需要在C++实现以下方法

int TinyToolSDK::setOrientation(int screen)
{
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
    JniMethodInfo minfo;
    if (JniHelper::getStaticMethodInfo(minfo, JAVA_CLASSNAME, "setOrientation", "(I)I"))
    {
        jint iret = minfo.env->CallStaticIntMethod(minfo.classID, minfo.methodID, screen);
        return iret;
    }
#elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS
    return GameIOS::getInstance()->setOrientation(screen);
#endif
    return 0;

}

四、使用lua调用C++

这里也有两种实现方法,一种是通过cocos的tolua工具,将类直接导出到lua使用,另外一种是利用lua_state将全局intC函数直接注册到lua使用,这两种方法,网上都有很多教程,不再赘述。

这里我们使用第一种,假设你已经将C++类导出到了lua,那么我们只需要在lua实现

ret = TinyToolSDK:setOrientation(screen)就可以了
到这里,我们已经实现了屏幕的翻转,但是对于我们游戏来说,还不够,因为我们屏幕翻转了,但是我们的设计分辨率还没有更改,我们的view还没有更改,所以我们需要最后一步操作,这里我就直接放上我的lua实现,供参考:
--设置屏幕方向screen,1:横屏,2:竖屏,3根据用户朝向
function setOrientation(screen)
    if VIEW_ORIENT == screen then return end
    local ret = -1
    ret = TinyToolSDK:setOrientation(screen)

    if ret == 0 then 
        VIEW_ORIENT = screen
            --view的分辨率
        local width , height
        if screen == 2 then 
            width = 640
            height = 1136
        elseif screen == 1 then 
            width = 1136 
            height = 640
        end
        local view = cc.Director:getInstance():getOpenGLView()
        view:setFrameSize(view:getFrameSize().height ,  view:getFrameSize().width)
        --重加载display
        package.loaded["cocos.framework.display"] = nil
        display    = require("cocos.framework.display")
    end
    return ret; 
end
至此,屏幕切换完成。
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值