原理:
1. 先在RenderTexture上渲染文字
2. 通过RenderTexture:newImage来获取到Image并依此得到对应的Texture
3 通过Texture构造Sprite,将生成的Sprite作为ClippingNode的模板(stencil)
4. ClippingNode的底图可以随意放置,这样我们就可以还不错的文字效果啦:)
-- 添加特效文本 -- example: addEffectText(self,300,400,"大家好",30,"1.png"); function addEffectText(parent,x,y,str,fontSize,bg) local target = cc.RenderTexture:create(640,50); target:retain(); target:setPosition(100,100); local text = cc.Label:createWithTTF(str,"fonts/simhei.ttf",fontSize); text:setColor(cc.c3b(255,0)); text:setPosition(320,25); target:begin(); text:visit(); target:endToLua(); local function copyImage() local pImage = target:newImage(); local tex = cc.Director:getInstance():getTextureCache():addImage(pImage,str); local sprite = cc.Sprite:createWithTexture(tex); local clippingNode = cc.ClippingNode:create(); local image = cc.Sprite:create(bg); clippingNode:setStencil(sprite); clippingNode:addChild(image); clippingNode:setAlphaThreshold(0.1); clippingNode:setPosition(x,y); parent:addChild(clippingNode); target:release(); end performWithDelay(parent,copyImage,0.01); end
效果: