在cocos2dx lua的项目中,难免会想修改屏幕的分辨率。我在修改分辨率这个问题上踩过坑,所以在这里记录一下。
修改屏幕分辨率可以在项目目录下的config.json的文件。
config.json文件如下:
{
"init_cfg": {
"isLandscape": true, //是否横屏,true:横屏,false:竖屏
"isWindowTop": false, //是否窗口置顶,true:置顶,false:不置顶
"name": "snakeGameLua", //窗口名称
"width": 1280, //窗口宽度
"height": 720, //窗口高度
"entry": "src/main.lua",//启动入口
"consolePort": 6050, //控制台端口
"uploadPort": 6060 //接收IDE上传文件的端口
},
"simulator_screen_size": [//模拟器屏幕分辨率的大小,只在桌面系统下生效
{
"title": "iPhone 3Gs (480x320)",
"width": 480,
"height": 320
},
{
"title": "iPhone 4 (960x640)",
"width": 960,
"height": 640
},
{
"title": "iPhone 5 (1136x640)",
"width": 1136,
"height": 640
},
{
"title": "iPad (1024x768)",
"width": 1024,
"height": 768
},
{
"title": "iPad Retina (2048x1536)",
"width": 2048,
"height": 1536
},
{
"title": "Android (800x480)",
"width": 800,
"height": 480
},
{
"title": "Android (854x480)",
"width": 854,
"height": 480
},
{
"title": "Android (1280x720)",
"width": 1280,
"height": 720
},
{
"title": "Android (1920x1080)",
"width": 1920,
"height": 1080
}
]
}
修改窗口分辨率的话,直接修改config.json下的窗口高度和窗口高度。
除了修改项目目录下的config.json文件之外,还需要修改项目目录下src文件夹下的config.lua,config.lua文件内容如下:
-- 0 - 不显示debug信息,1 - 打印少量debug信息,2 - 打印标准debug信息
-- 0 - disable debug info, 1 - less debug info, 2 - verbose debug info
DEBUG = 2
-- 使用框架,将禁用所有过时的API,false - 用过时的API
-- use framework, will disable all deprecated API, false - use legacy API
CC_USE_FRAMEWORK = true
-- 屏幕显示FPS
-- show FPS on screen
CC_SHOW_FPS = true
-- 禁止未预期的全局变量
-- disable create unexpected global variable
CC_DISABLE_GLOBAL = true
--设计分辨率
-- for module display
CC_DESIGN_RESOLUTION = {
width = 1280,
height = 720,
autoscale = "FIXED_HEIGHT",
callback = function(framesize)
local ratio = framesize.width / framesize.height
if ratio <= 1.34 then
-- iPad 768*1024(1536*2048) is 4:3 screen
return {autoscale = "FIXED_WIDTH"}
end
end
}
config.lua内需要修改的是width和height的值。如果只修改config.json而不修改config.lua内的值,会出现图片的显示大小和设置的大小不相同的问题。
第一次见到这个问题的时候,一直想不通为啥自己设置的图片大小(debug出来的数据和设置的图片大小相同)和显示的实际大小不同。