cocos2dx怎么得到一个指定layer
1个回答
2015-02-12 · 知道合伙人影视综艺行家
关注
展开全部
--创建一个touch的半透明layer
--priority : touch 权限级别,默认为-1024
--touchRect: 在touchRect 区域会放行touch事件 若touchRect = nil 则全屏吃touch
--touchCallback: 屏蔽层touch 回调
function createMaskLayer( priority,touchRect ,touchCallback, layerOpacity,highRect)
local layer = CCLayer:create()
layer:setPosition(ccp(0, 0))
layer:setAnchorPoint(ccp(0, 0))
layer:setTouchEnabled(true)
layer:setTouchPriority(priority or -1024)
layer:registerScriptTouchHandler(function ( eventType,x,y )
if(eventType == "began") then
if(touchRect == nil) then
if(touchCallback ~= nil) then
touchCallback()
end
return true
else
if(touchRect:containsPoint(ccp(x,y))) then
return false
else
if(touchCallback ~= nil) then
touchCallback()
end
return true
end
end
end
print(eventType)
end,false, priority or -1024, true)
local gw,gh = g_winSize.width, g_winSize.height
if(touchRect == nil) then
local layerColor = CCLayerColor:create(ccc4(0,0,0,layerOpacity or 150),gw,gh)
layerColor:setPosition(ccp(0,0))
layerColor:setAnchorPoint(ccp(0,0))
layer:addChild(layerColor)
return layer
else
local ox,oy,ow,oh = touchRect.origin.x, touchRect.origin.y, touchRect.size.width, touchRect.size.height
local layerColor = CCLayerColor:create(ccc4(0, 0, 0, layerOpacity or 150 ), gw, gh)
local clipNode = CCClippingNode:create();
clipNode:setInverted(true)
clipNode:addChild(layerColor)
local stencilNode = CCNode:create()
-- stencilNode:retain()
local node = CCScale9Sprite:create("bg.png");
node:setContentSize(CCSizeMake(ow, oh))
node:setAnchorPoint(ccp(0, 0))
node:setPosition(ccp(ox, oy))
stencilNode:addChild(node)
if(highRect ~= nil) then
local highNode = CCScale9Sprite:create("bg.png");
highNode:setContentSize(CCSizeMake(highRect.size.width, highRect.size.height))
highNode:setAnchorPoint(ccp(0, 0))
highNode:setPosition(ccp(highRect.origin.x, highRect.origin.y))
stencilNode:addChild(highNode)
end
clipNode:setStencil(stencilNode)
clipNode:setAlphaThreshold(0.5)
layer:addChild(clipNode)
end
return layer
end
--priority : touch 权限级别,默认为-1024
--touchRect: 在touchRect 区域会放行touch事件 若touchRect = nil 则全屏吃touch
--touchCallback: 屏蔽层touch 回调
function createMaskLayer( priority,touchRect ,touchCallback, layerOpacity,highRect)
local layer = CCLayer:create()
layer:setPosition(ccp(0, 0))
layer:setAnchorPoint(ccp(0, 0))
layer:setTouchEnabled(true)
layer:setTouchPriority(priority or -1024)
layer:registerScriptTouchHandler(function ( eventType,x,y )
if(eventType == "began") then
if(touchRect == nil) then
if(touchCallback ~= nil) then
touchCallback()
end
return true
else
if(touchRect:containsPoint(ccp(x,y))) then
return false
else
if(touchCallback ~= nil) then
touchCallback()
end
return true
end
end
end
print(eventType)
end,false, priority or -1024, true)
local gw,gh = g_winSize.width, g_winSize.height
if(touchRect == nil) then
local layerColor = CCLayerColor:create(ccc4(0,0,0,layerOpacity or 150),gw,gh)
layerColor:setPosition(ccp(0,0))
layerColor:setAnchorPoint(ccp(0,0))
layer:addChild(layerColor)
return layer
else
local ox,oy,ow,oh = touchRect.origin.x, touchRect.origin.y, touchRect.size.width, touchRect.size.height
local layerColor = CCLayerColor:create(ccc4(0, 0, 0, layerOpacity or 150 ), gw, gh)
local clipNode = CCClippingNode:create();
clipNode:setInverted(true)
clipNode:addChild(layerColor)
local stencilNode = CCNode:create()
-- stencilNode:retain()
local node = CCScale9Sprite:create("bg.png");
node:setContentSize(CCSizeMake(ow, oh))
node:setAnchorPoint(ccp(0, 0))
node:setPosition(ccp(ox, oy))
stencilNode:addChild(node)
if(highRect ~= nil) then
local highNode = CCScale9Sprite:create("bg.png");
highNode:setContentSize(CCSizeMake(highRect.size.width, highRect.size.height))
highNode:setAnchorPoint(ccp(0, 0))
highNode:setPosition(ccp(highRect.origin.x, highRect.origin.y))
stencilNode:addChild(highNode)
end
clipNode:setStencil(stencilNode)
clipNode:setAlphaThreshold(0.5)
layer:addChild(clipNode)
end
return layer
end
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询