cocos2dx 遮罩層 android 手機上 失敗

來源:互聯網
上載者:User

標籤:cocos2dx   遮罩   android   

1.CCClippingNode使用(在模擬器上ok,在手機上不行),實現多個剪下地區

 local layer=CCLayerColor:create(ccc4(0,0,0,110))
    --/*建立剪裁節點*/
    local pClip=CCClippingNode:create()
    pClip:setInverted(true)--;//設定是否反向,將決定畫出來的圓是透明的還是黑色的
    pClip:setAlphaThreshold(0.0)
    self:addChild(pClip)
    --/*注意將LayerColor層添加到剪裁節點上*/
    pClip:addChild(layer)

local stencilNode = display.newNode()
    stencilNode:setAnchorPoint(ccp(0,0))
    stencilNode:setPosition(ccp(0,0))

  --/*繪製圓形地區*/
    local radius=NODE_PADDING/2+10--;//設定圓的半徑
    local nCount=200--;//設定頂點數,此處我們將圓看成200邊型
    local angel=2.0*3.14/nCount
    local points = CCPointArray:create(nCount)
    for i=0, nCount-1 do
        local radian=i*angel
        points:add(ccp(radius * math.cos(radian),radius * math.sin(radian)))
    end

     --/*通過繪製多邊形來實現圓*/

    -- 建立 CCPolygonShape 對象
    local stencil_1 = CCPolygonShape:create(points)  
    stencil_1:setFill(true) -- 填充
    stencil_1:setClose(true) --閉合
    stencil_1:setPosition(ccp(x1, y1))
    stencilNode:addChild(stencil_1)

--增加一個圖片的剪下地區

local   stencil_2= display.newSprite("light.png")

 stencil_1:setPosition(ccp(x2, y2))

stencilNode:addChild(stencil_2)

pClip:setStencil(stencilNode)


2.重點來了,這個才可以在手機上用

function game:mask()
    local function setBlend(obj, src, dst)
        local b = ccBlendFunc:new()
        b.src = src
        b.dst = dst
        obj:setBlendFunc(b)
    end
     --背景
    local node = display.newNode():zorder(10000)
    local layer = display.newColorLayer(ccc4(0,0,0,0))
    node:addChild(layer)
    --node:setContentSize(CCSize(1000, 1000))
    local myLayer = display.newColorLayer(ccc4(0,0,0,175))
            --建立遮罩圖片
    local pMask
    pMask = display.newSprite("#bun_1_1.png")
    pMask:setScaleX(1.5)
    pMask:setScaleY(1.5)--設定一下縮放
    pMask:setAnchorPoint(ccp(0,0))
    pMask:setPosition(display.cx, display.cy)
            --設定混合模式
    setBlend(pMask, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA)


    --建立乾淨的畫板
    local pRt = CCRenderTexture:create(CONFIG_SCREEN_WIDTH, CONFIG_SCREEN_HEIGHT)
    self:addChild(pRt);
    pRt:setPosition(CONFIG_SCREEN_WIDTH/2, CONFIG_SCREEN_HEIGHT/2)
            --開始繪製
    pRt:begin()
    myLayer:visit()
    --if pData then
        pMask:visit()
    --end
    pRt:endToLua()
    --self:addChild(myLayer)


    -- 啟用觸摸
   -- function onTouch(event, x, y)
    --echoInfo(string.format("# onTouch111 X      = %s", x))
   -- echoInfo(string.format("# onTouch 111Y      = %s", y))
   -- if x > display.cx - 100 and x < display.cx + 100 and y > display.cy - 100 and y < display.cy + 100 then
    --    node:removeFromParent()
    --    pRt:removeFromParent()
    --end
    --return true
  --  end
    --開啟觸摸
   -- node:setTouchEnabled(true)
    --node:addTouchEventListener(onTouch)
    --node:setNodeEventEnabled(true)
    --self:addChild(node,1000)
end




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.