cocos2dx3.17.2橫豎屏切換

在游戲中需要橫豎屏切換,常用大廳到子游戲

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);  //這一步必須設(shè)置,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); //這一步必須設(shè)置,glview 的大小為RootViewController
    }
}
  1. 到這里ios的設(shè)置基本上完成了,可以在Lua里調(diào)用了。最后面貼lua代碼

Android

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

  2. 添加靜態(tài)方法

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);
    }
}

腳本調(diào)用, 注意Lua調(diào)用oc方法的時候傳參數(shù)必須是字典的形式傳遞

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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容