原文http://cn.quick-x.com/?p=1539 是基于quick-x的
为了方便在我们工程中使用,我把它写成了cocos2dx的,并增加了吞噬事件的开关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
function setShade(sprite,swallow) swallow = swallow or false local spriteCp = CCSprite:createWithSpriteFrame(sprite:displayFrame()) spriteCp:setAnchorPoint(sprite:getAnchorPoint()) spriteCp:setPosition(ccp(sprite:getPosition())) local b = ccBlendFunc:new() b.src = GL_ZERO b.dst = GL_ONE_MINUS_SRC_ALPHA spriteCp:setBlendFunc(b) local maskLayer = CCLayerColor:create(ccc4(0,0,0,255*0.7)) local maskRt = CCRenderTexture:create(visibleSize.width, visibleSize.height) maskRt:setPosition(visibleSize.width/2, visibleSize.height/2) maskRt:begin() maskLayer:visit() spriteCp:visit() maskRt:endToLua() if swallow then local boundingBox = spriteCp:boundingBox() maskRt:setTouchEnabled(true) maskRt:registerScriptTouchHandler(function(e,x,y) log.info("touch",e,x,y) if boundingBox:containsPoint(ccp(x,y)) then return false end return true end,false,true) end return maskRt end |