復制對象紋理


--復制對象紋理
function g_CutPicture(pContainer)
    local winSize = cc.Director:getInstance():getWinSize();
    local aniSize = pContainer:getContentSize();
    --aniSize = cc.size(120,200);
    local aniAnchorPoint = cc.p( pContainer:getAnchorPoint()  );
    --print(aniAnchorPoint.x,aniAnchorPoint.y);
    aniAnchorPoint.x = 0.5;
    local worldPos = pContainer:getParent():convertToWorldSpace( cc.p( pContainer:getPosition() ) );

    local cutSizeWidth   = worldPos.x + aniSize.width*(1 - aniAnchorPoint.x);
    local cutSizeHeight  = worldPos.y + aniSize.height*(1 - aniAnchorPoint.y);

    -- cutSizeWidth = cutSizeWidth > 0 and cutSizeWidth or -1;
    -- cutSizeHeight = cutSizeHeight > 0 and cutSizeHeight or -1;
    -- print(cutSizeWidth,cutSizeHeight);

    local rt = nil; 
    if cutSizeWidth > 0 and cutSizeHeight > 0 then
        rt = cc.RenderTexture:create(  cutSizeWidth  ,  cutSizeHeight );
    else
        print("-----------------------------------------");
        print(pContainer:getPositionX(),pContainer:getPositionY());
        print( "worldPos.x = "..worldPos.x.."; worldPos.y = "..worldPos.y );
        print(cutSizeWidth,cutSizeHeight);
    end

    if rt  then
        
        rt:retain();
        rt:beginWithClear(255,255,255,0); 
        --pContainer:visit();
        --繪制所有節(jié)點
        function drawAllNode( node )
            if node:getChildrenCount() > 0 and node:isVisible() == true then
                for k,v in pairs( node:getChildren() ) do
                     drawAllNode(v);
                    -- v:visit();
                end
            end
            if node:isVisible() == true then
                node:visit();
            end
        end
        drawAllNode(pContainer);
        rt:endToLua();
       
        
        local texture = rt:getSprite():getTexture(); 

        local cutX  = worldPos.x - aniSize.width*aniAnchorPoint.x;
        local maxX  = aniSize.width ;
        -- if cutX < 0 then
        --  --maxX = maxX + cutX;
        --  --cutX = 0;
        -- end

        local cutY  = worldPos.y - aniSize.height*aniAnchorPoint.y;
        local maxY  = aniSize.height ;
        -- if maxY < 0 then
        --  --maxY = maxY + cutY;
        --  --cutY = 0;
        -- end

        local texRect = cc.rect( cutX,
                        cutY,
                        maxX,
                        maxY );
        -- for k,v in pairs(texRect) do
        --  print(k,v)
        -- end
        if texture then
            local pSprite = cc.Sprite:createWithTexture(texture,texRect  );
            pSprite:setFlippedY(true);
            pSprite:setAnchorPoint( aniAnchorPoint );
            return pSprite; 
        end
        rt:release();
        rt = nil
    end

    return nil;
    
end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在js中,我們經(jīng)常復制一個對象,復制數(shù)據(jù),那么就會有人問了,怎么復制? JS中對象分為基本類型和復合(引用)類型,...
    虛幻的銹色閱讀 38,560評論 6 30
  • 在實際開發(fā)中,對于一些自定義的對象,有時我們也希望對其進行復制。對于自定義對象的復制,首先要保證在類的定義中遵守N...
    趙亦晨閱讀 1,898評論 0 0
  • 前言 從層次上來看,對象的復制可以簡單地分為淺復制和深復制,顧名思義,淺復制是指只復制一層對象的屬性,不會復制對象...
    Claiyre閱讀 253評論 0 1
  • 注意從數(shù)組中拿數(shù)據(jù)傳輸,地址傳遞造成的原數(shù)據(jù)污染! 自定義對象的深拷貝要實現(xiàn)NSCoping方法。 @interf...
    然亦傘閱讀 470評論 0 0
  • 客戶突然的向我推薦了幾本書。這是什么情況?我反應(yīng)遲鈍。 原來她也喜歡看書。 現(xiàn)在很少年輕人喜歡看書!可是我不年輕了...
    流浪癡人閱讀 432評論 2 1

友情鏈接更多精彩內(nèi)容