在游戲中需要橫豎屏切換,常用大廳到子游戲
IOS
- 修改
Application.mm
, 首先定義變量
static RootViewController* s_rv;
UIInterfaceOrientationMask orientation = UIInterfaceOrientationMaskLandscape;
- 在
didFinishLaunchingWithOptions
中對s_rv
進行賦值操作
s_rv = _viewController;
- 增加下面兩個方法
- (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
}
}
- 到這里ios的設置基本上完成了咪惠,可以在Lua里調用了。最后面貼lua代碼
Android
修改
AppActivity
, 首先定義變量
public static AppActivity instance = null;
并在onCreate
中初始化變量為instance = this
添加靜態(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, "切換豎屏");
}
}
- 修改
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);
}
- 修改
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方法的時候傳參數(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