项目是使用LUA,所以需要对应的在android和ios双平台分别做切换处理,在通过lua,调用不同的平台处理。 一、android平台 //设置手机的旋转方向1:横屏,2:竖屏,3根据用户朝向 2)当orientation为2的时候,第一个设置为设置竖屏,不随重力感应器切换方向。第二个为根据手机重力感应,设置竖屏的上下方向 设置完之后,android处理方法添加完毕,我们再去IOS里面添加对应的IOS代码 二、IOS平台 int GameIOS::setOrientation(int screen) static bool g_bIsPortrait = false; – (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window +(void)setOrientation:(bool)bIsPortrait{ 三、使用C++调用android和IOS 我们只需要在C++实现以下方法 int TinyToolSDK::setOrientation(int screen) } 四、使用lua调用C++ 这里也有两种实现方法,一种是通过cocos的tolua工具,将类直接导出到lua使用,另外一种是利用lua_state将全局intC函数直接注册到lua使用,这两种方法,网上都有很多教程,不再赘述。 这里我们使用第一种,假设你已经将C++类导出到了lua,那么我们只需要在lua实现 ret = TinyToolSDK:setOrientation(screen)就可以了 if ret == 0 then
1、首先我们需要在android的AppActivity.java添加如下代码
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的时候,第一个设置为设置横屏,不随重力感应器切换方向。第二个为根据手机重力感应,设置横屏的上下方向
首先我们添加一个转换方法,将lua即将传过来的方向的值,转化为ios里面对应的Boolean值
{
if(screen == 1)
{
[AppController setOrientation:false];
}
else if(screen == 2)
{
[AppController setOrientation:true];
}
return 0;
}
然后,我们需要在AppContriller.mm里面,实现这个setOrientation方法,代码如下
{
if (g_bIsPortrait == false) {
//横屏
return UIInterfaceOrientationMaskLandscape;
}else{
//竖屏
return UIInterfaceOrientationMaskPortrait;
}
}
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”];
}
OK,到这里,我们android和IOS端的实现都已经实现,接下来要做的,就是在lua里面去调用这些实现,有两种方式,一种使用lua直接调用,一种是使用C++分别调用android和IOS,我们这里使用第一种
{
#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;
到这里,我们已经实现了屏幕的翻转,但是对于我们游戏来说,还不够,因为我们屏幕翻转了,但是我们的设计分辨率还没有更改,我们的view还没有更改,所以我们需要最后一步操作,这里我就直接放上我的lua实现,供参考:
–设置屏幕方向screen,1:横屏,2:竖屏,3根据用户朝向
function setOrientation(screen)
if VIEW_ORIENT == screen then return end
local ret = -1
ret = TinyToolSDK:setOrientation(screen)
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
至此,屏幕切换完成。
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算