參考文章:http://zengrong.net/post/2067.htm點擊打開鏈接
1.準備資源
背景圖 background.jpg
遮蓋圖 HelloWorld.png
橡皮檫圖片 test1.png 中間透明的圖。 BlendFunc爲 cc.blendFunc(GL_ZERO, GL_SRC_ALPHA)
如果是實心外透明 BlendFunc爲 cc.blendFunc(GL_ZERO, GL_ONE_MINUS_SRC_ALPHA)
這裏最主要還是對glBlendFunc的知識的運用,如果不想知道原理可以無視。
2. code
local background = cc.Sprite:create("background.jpg")
:pos(display.cx,display.cy)
:addTo(self)
background:scale(display.height / background:getContentSize().height)
local dirt = cc.Sprite:create("HelloWorld.png")
:pos(display.cx,display.cy)
dirt:scale(display.width / dirt:getContentSize().width)
dirt:retain();
local eraser = cc.Sprite:create("test1.png")
:pos(display.cx, display.cy)
:scale(10)
eraser:retain()
eraser:setBlendFunc(cc.blendFunc(GL_ZERO, GL_SRC_ALPHA));
local renderTexture = cc.RenderTexture:create(display.width,display.height)
:pos(display.cx,display.cy)
:addTo(self)
renderTexture:begin();
dirt:visit();
renderTexture:endToLua(); --todo
self:onTouch(function(event)
eraser:pos(event.x, event.y)
renderTexture:begin();
-- dirt:visit(); --電筒效果
eraser:visit();
renderTexture:endToLua(); --todo
return true;
end):enableTouch(true)
4.效果圖 橡皮檫 註釋onTouch 裏的dirt:visit