Cpp Lua 用小图块拼接大背景图片

欢迎各位童鞋转载,转载请注明出处:http://blog.csdn.net/song_hui_xiang

作者新浪微博:http://weibo.com/u/3168848533

作者腾讯微博:http://t.qq.com/song_huixiang

Lua:

--在游戏开发中图片资源是能省则省的,对于多处使用而且色调单一像对话框这样的一般都会采取以小图块拼接的方式来实现,以达到节省资源的目的。  下面是实现拼接方法
--拼接背景框  _x,_y;起始坐标(以整体图左上角为起点)   _width:宽    _height:高   _corner_file_name:左上角块图片   _side_file_name:上边块图片   _center_file_name:中心块图片   3个图片资源必须是大小一样的正方形
local function combineBackground(_x,_y,_width,_height,_corner_file_name,_side_file_name,_center_file_name)
    local layer = CCLayer:create()
    
    local center = CCSprite:create(_center_file_name)
    
    local distance = center:getContentSize().width
    
    --都取整数以防拼接有缝隙
    local horizontal_num = math.floor(_width/distance)  --已知宽度算出总列数
    local vertical_num = math.floor(_height/distance)   --已知高度算出总行数
    local _x = math.floor(_x)
    local _y = math.floor(_y)
    
    for i=1,vertical_num do
        for j=1,horizontal_num do
            if i==1 and j==1 then
               local left_up = CCSprite:create(_corner_file_name)
               left_up:setPosition(ccp(_x,_y))
               layer:addChild(left_up)
            elseif i==1 and j==horizontal_num then
                local right_up = CCSprite:create(_corner_file_name)
                right_up:setRotation(90)
                right_up:setPosition(ccp(_x+(horizontal_num-1)*distance,_y))
                layer:addChild(right_up)
            elseif i==1 and j~=1 and j~=horizontal_num then
                local up_side = CCSprite:create(_side_file_name)
                up_side:setPosition(ccp(_x+(j-1)*distance,_y))
                layer:addChild(up_side)
            elseif i>=2 and i<=vertical_num-1 then
                if j==1 then
                    local left_side = CCSprite:create(_side_file_name)
                    left_side:setPosition(ccp(_x+(j-1)*distance,_y-(i-1)*distance))
                    left_side:setRotation(-90)
                    layer:addChild(left_side)
                elseif j==horizontal_num then
                    local right_side = CCSprite:create(_side_file_name)
                    right_side:setPosition(ccp(_x+(j-1)*distance,_y-(i-1)*distance))
                    right_side:setRotation(90)
                    layer:addChild(right_side)
                else
                    local center = CCSprite:create(_center_file_name)
                    center:setPosition(ccp(_x+(j-1)*distance,_y-(i-1)*distance))
                    layer:addChild(center)
                end
            elseif i==vertical_num then
                if j==1 then
                    local right_down = CCSprite:create(_corner_file_name)
                    right_down:setRotation(-90)
                    right_down:setPosition(ccp(_x+(j-1)*distance,_y-(i-1)*distance))
                    layer:addChild(right_down)
                elseif j==horizontal_num then
                    local left_down = CCSprite:create(_corner_file_name)
                    left_down:setRotation(180)
                    left_down:setPosition(ccp(_x+(j-1)*distance,_y-(i-1)*distance))
                    layer:addChild(left_down)
                else
                    local down_side = CCSprite:create(_side_file_name)
                    down_side:setRotation(180)
                    down_side:setPosition(ccp(_x+(j-1)*distance,_y-(i-1)*distance))
                    layer:addChild(down_side)
                end
            end
        end
    end
    return layer
end


Cpp:

CCLayer* Utilities::combineBackground(int _x,int _y,float _width,float _height,string _corner_file_name,string _side_file_name,string _center_file_name)
{
    CCLayer* layer = CCLayer::create();
    CCSprite* center = CCSprite::createWithSpriteFrameName(_center_file_name.c_str());
    float distance = center->getContentSize().width;
    
    //都取整数以防拼接有缝隙
    int horizontal_num = (int)(_width/distance);  //已知宽度算出总列数
    int vertical_num = (int)(_height/distance);   //已知高度算出总行数
    
    for (int i=1; i<=vertical_num; i++) {
        for (int j=1; j<=horizontal_num; j++) {
            if (i==1 && j==1) {
                CCSprite* left_up = CCSprite::createWithSpriteFrameName(_corner_file_name.c_str());
                left_up->setPosition(ccp(_x, _y));
                layer->addChild(left_up);
            }else if (i==1 && j==horizontal_num){
                CCSprite* right_up = CCSprite::createWithSpriteFrameName(_corner_file_name.c_str());
                right_up->setRotation(90);
                right_up->setPosition(ccp(_x+(horizontal_num-1)*distance,_y));
                layer->addChild(right_up);
            }else if (i==1 && j!= 1 && j!=horizontal_num){
                CCSprite* up_side = CCSprite::createWithSpriteFrameName(_side_file_name.c_str());
                up_side->setPosition(ccp(_x+(j-1)*distance,_y));
                layer->addChild(up_side);
            }else if (i>=2 && i<=vertical_num-1){
                if (j==1) {
                    CCSprite* left_side = CCSprite::createWithSpriteFrameName(_side_file_name.c_str());
                    left_side->setPosition(ccp(_x+(j-1)*distance,_y-(i-1)*distance));
                    left_side->setRotation(-90);
                    layer->addChild(left_side);
                }else if (j==horizontal_num){
                    CCSprite* right_side = CCSprite::createWithSpriteFrameName(_side_file_name.c_str());
                    right_side->setPosition(ccp(_x+(j-1)*distance,_y-(i-1)*distance));
                    right_side->setRotation(90);
                    layer->addChild(right_side);
                }else{
                    CCSprite* center = CCSprite::createWithSpriteFrameName(_center_file_name.c_str());
                    center->setPosition(ccp(_x+(j-1)*distance,_y-(i-1)*distance));
                    layer->addChild(center);
                }
            }else if (i==vertical_num){
                if (j==1) {
                    CCSprite* right_down = CCSprite::createWithSpriteFrameName(_corner_file_name.c_str());
                    right_down->setRotation(-90);
                    right_down->setPosition(ccp(_x+(j-1)*distance,_y-(i-1)*distance));
                    layer->addChild(right_down);
                }else if (j==horizontal_num){
                    CCSprite* left_down = CCSprite::createWithSpriteFrameName(_corner_file_name.c_str());
                    left_down->setRotation(180);
                    left_down->setPosition(ccp(_x+(j-1)*distance,_y-(i-1)*distance));
                    layer->addChild(left_down);
                }else{
                    CCSprite* down_side = CCSprite::createWithSpriteFrameName(_side_file_name.c_str());
                    down_side->setRotation(180);
                    down_side->setPosition(ccp(_x+(j-1)*distance,_y-(i-1)*distance));
                    layer->addChild(down_side);
                }
            }
        }
    }
    return layer;
}



左上角块图片: 上边块图片: 中心块图片: 

效果图:

Cpp Lua 用小图块拼接大背景图片_第1张图片



你可能感兴趣的:(Cpp Lua 用小图块拼接大背景图片)