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

手把手教你Cocos2D-X游戏中屏幕横竖屏转换的实现方法作者:课课家教育&http://www.kokojia.com点击数:11373发布时间: 09:31:59  我们在进行的过程中经常会遇到游戏屏幕需要横竖屏转换的情形,你还在烦恼该怎么解决这个问题吗?快来看看这篇教程吧,本篇教程将为你解读游戏中屏幕横竖屏转换的实现方法。
  解决方案:
  1.在游戏的主activity中编写一个静态方法(继承Cocos2dxActivity)
  2.在需要切换横竖屏的代码中通过JNI调用changedActivityOrientation方法,如下所示
  开发中遇见的问题:
  部分手机进行横竖屏切换正常,部分设备切换时崩溃
  1.检查AndroidManifest.文件中是否有android:targetSdkVersion="18" 选项,移去该选项重新打包测试。
  (估计只要设备android系统与该选项指定的API版本相同安装该应用才不会崩溃,移除该选项后其它设备方可正常运行)标签:赞(17)踩(6)分享到:上一篇:下一篇:最新教程热门教程评论()您需要登录后才可以评论请[][]最新评论暂无评论~游戏开发为你推荐推荐查看热门资讯热门图书扫一扫体验手机阅读
【cocos2d-x】横向滚屏射击游戏②----虚拟控制手柄
<span type="1" blog_id="998507" userid='
37篇文章,12W+人气,1粉丝在config.json,
"init_cfg": {
"isLandscape": false,
"isWindowTop": false,
"name": "CocosLuaGame",
"width": 960,
"height": 640,
"entry": "src/main.lua",
"consolePort": 6010,
"uploadPort": 6020,
"forwardConsolePort": 10089,
"forwardUploadPort": 10091
}在isLandsscape中,false代表竖屏,true代表横屏。
在Cocos2d-x上实现横竖屏切换
cocos2d 屏幕默认是横屏,修改为竖屏 的方法
横竖屏切换 cocos2dx
Cocos Code IDE 开发Lua和Cocos2d-x3.2配置篇(可能是最新的教程)
没有更多推荐了,Access denied | www.bkjia.com used Cloudflare to restrict access
Please enable cookies.
What happened?
The owner of this website (www.bkjia.com) has banned your access based on your browser's signature (eb9847-ua98).最近在做一个棋牌项目,有个需求,是当点击某个按钮的时候,游戏从横屏转到竖屏,再次点击的时候,游戏从竖屏再转到横屏。之前写过一个更改项目横竖屏的博客,但是那个只是在确定项目屏幕方向的时候设置。这次的更改,与上次不同。进入正题:项目是使用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);
}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];
}然后,我们需要在AppContriller.mm里面,实现这个setOrientation方法,代码如下static bool g_bIsPortrait =
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
if (g_bIsPortrait == false) {
return UIInterfaceOrientationMaskL
return UIInterfaceOrientationMaskP
+(void)setOrientation:(bool)bIsPortrait{
if(g_bIsPortrait == bIsPortrait){
g_bIsPortrait = bIsP
UIInterfaceOrientation interfaceOrientation = UIInterfaceOrientationU
if(bIsPortrait){
interfaceOrientation =UIInterfaceOrientationP
interfaceOrientation =UIInterfaceOrientationLandscapeR
//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]];
// 从2开始是因为0 1 两个参数已经被selector和target占用
[invocation setArgument:&val atIndex:2];
[invocation invoke];
g_bIsPortrait = bIsP
UIInterfaceOrientation interfaceOrientation = UIInterfaceOrientationU
if(bIsPortrait){
interfaceOrientation =UIInterfaceOrientationP
interfaceOrientation =UIInterfaceOrientationLandscapeL
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
JniMethodI
if (JniHelper::getStaticMethodInfo(minfo, JAVA_CLASSNAME, "setOrientation", "(I)I"))
jint iret = minfo.env-&CallStaticIntMethod(minfo.classID, minfo.methodID, screen);
#elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS
return GameIOS::getInstance()-&setOrientation(screen);
}四、使用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
local view = cc.Director:getInstance():getOpenGLView()
view:setFrameSize(view:getFrameSize().height ,
view:getFrameSize().width)
--重加载display
package.loaded["cocos.framework.display"] = nil
= require("cocos.framework.display")
end end至此,屏幕切换完成。
与屏幕有关的设置:禁止横竖屏切换;横竖屏切换不重启Activity;动态设置横竖屏
cocos2d-x游戏开发屏幕横竖屏切换
cocos2d怎么设置屏幕朝向?横屏 or 竖屏设置
cocos2d-x如何切换横竖屏
没有更多推荐了,

我要回帖

更多关于 cocos 的文章

 

随机推荐