cocos2d-x自适应android不同分辨率,使用多套资源

博客介绍了使用cocos2d-x实现Android不同分辨率自适应的方法,选择了9:16、3:5、2:3、3:4四种分辨率,在resource目录下新建文件夹放置多套背景图片,在AppDelegate中添加函数调用。还介绍了setSearchPaths函数的使用及相关描述。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

虽然 cocos2d-x自带了三种模式来适应屏幕,但还是存在一些问题,会产生黑边,所以还是需要多套资源来适应屏幕。
这里我选择了四种分辨率9:16 3:5 2:3 3:4,在resource资源目录下新建了四个文件夹放置多套背景图片,在AppDelegate中添加如下函数调用即可。
[cpp] 
void AppDelegate::ScreenAdaptive()  
{  
    CCSize szFrame = CCEGLView::sharedOpenGLView()->getFrameSize();  
    float Proportion = szFrame.width/szFrame.height;  
    const int num = 4;  
    float diff[num]={fabs(Proportion-(9.0/16)),  
        fabs(Proportion-(3.0/5)),fabs(Proportion-(2.0/3)),fabs(Proportion-(3.0/4))};  
    int yy = -1;  
    for (int i=0;i<num;i++)  
        if((diff[i] >-0.000001) && (diff[i] < 0.000001))  
        {  
            yy = i;  
            break;  
        }  
    if (yy == -1)  
    {  
        int min = diff[0];  
        yy = 0;  
        for (int i=1;i<num;i++)  
            if (diff[i]<min)  
            {  
                min = diff[i];  
                yy = i;  
            }  
    }  
    switch(yy)  
    {  
    case 0:  
        {  
            std::vector<string> searchPaths;  
            searchPaths.push_back("9x16");  
            CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);  
            CCEGLView::sharedOpenGLView()->setDesignResolutionSize(648,1152, kResolutionShowAll);  
            break;  
        }  
    case 1:  
        {  
            std::vector<string> searchPaths;  
            searchPaths.push_back("3x5");  
            CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);  
            CCEGLView::sharedOpenGLView()->setDesignResolutionSize(624,1040, kResolutionShowAll);  
            break;  
        }  
    case 2:  
        {  
            std::vector<string> searchPaths;  
            searchPaths.push_back("2x3");  
            CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);  
            CCEGLView::sharedOpenGLView()->setDesignResolutionSize(640,960, kResolutionShowAll);  
            break;  
        }  
    case 3:  
        {  
            std::vector<string> searchPaths;  
            searchPaths.push_back("3x4");  
            CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);  
            CCEGLView::sharedOpenGLView()->setDesignResolutionSize(624,832, kResolutionShowAll);  
            break;  
        }  
    }  
}  
这里简单介绍一下setSearchPaths,在最近更新的版本里,已经没有了setResourceDirectory,取而代之的是setSearchPaths,以下是关于它的描述:
[cpp]  
/**  
 *  Sets the array of search paths. 
 *  
 *  You can use this array to modify the search path of the resources. 
 *  If you want to use "themes" or search resources in the "cache", you can do it easily by adding new entries in this array. 
 * 
 *  @note This method could access relative path and absolute path. 
 *        If the relative path was passed to the vector, CCFileUtils will add the default resource directory before the relative path. 
 *        For instance: 
 *         On Android, the default resource root path is "assets/". 
 *         If "/mnt/sdcard/" and "resources-large" were set to the search paths vector, 
 *         "resources-large" will be converted to "assets/resources-large" since it was a relative path. 
 * 
 *  @param searchPaths The array contains search paths. 
 *  @see fullPathForFilename(const char*) 
 *  @since v2.1 
 */  
 
[cpp] 
// set searching paths to "/mnt/sd/example" and "/data/data/org.cocos2dx.example"   
vector<string> searchPaths;  
searchPaths.push_back("/mnt/sd/example");  
searchPaths.push_back("/data/data/org.cocos2dx.example");  
CCFileUtils::setSearchPaths(searchPaths);    
// engine will find "1.png" in /mnt/sd/example, if there it is not found, then engine will find "1.png" in /data/data/org.cocos2dx.example  
// if not found, engine will find "1.png" in Resources/ (this path is platform dependent)  
 
[cpp]  
CCSprite *pSprite = CCSprite

 

转载地址: http://www.2cto.com/kf/201311/256694.html
一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值