quick cocos设置父parent,setParent的用法
我们在使用cocos的时候,有的时候需要把子node(当然包含所有继承自node 的组件)从父node中移除,方便复用,减少内存,cpu的开销。好了,废话不多说,直接上代码:
nodeChildren:getParent():getChildren():removeObject(nodeChildren,false);
nodeChildren:setParent(nil);
nodeChildren:setTag(1)
nodeChildren:setPosition(ccp(0,0))
nodeNew:addChild(nodeChildren);
上面是实现圆角图片的基础,复用图片剪切圆角,不说了直接贴代码
--imgP=原始图片 pw=图片宽 py=图片高 tagImg=原来的图片tag(检查重复用)
function getRoundImg(imgP,pw,py,tagImg)
if imgP == nil or tolua.isnull(imgP) then return end
tagImg = tagImg or 0
if imgP:getParent():getTag() == tagImg+100 then
cclog("getRoundImg have already add clippnode") --防止重复添加导致创建了多个clippnode
return imgP
end
pw = pw or 200
py = py or 200
local px,py=imgP:getPositionX(),imgP:getPositionY()
local pa = imgP:getAnchorPoint()
local pw,ph = imgP:getContentSize().width * imgP:getScaleX(),imgP:getContentSize().height * imgP:getScaleY()
local stencil = CCSprite:create()
local spriteFrameCache = CCSpriteFrameCache:sharedSpriteFrameCache()
spriteFrameCache:addSpriteFramesWithFile('res/圆角图片的.plist')
local rBg = spriteFrameCache:spriteFrameByName("圆角图片名.png")
stencil:setDisplayFrame(rBg)
stencil:setAnchorPoint(pa)
stencil:setScaleX(pw/200)
stencil:setScaleY(pw/200)
local clipNode = CCClippingNode:create()
clipNode:setStencil(stencil)
clipNode:setInverted(false)
clipNode:setAlphaThreshold(0.05)
clipNode:setAnchorPoint(pa)
clipNode:setPosition(ccp(px,py))
clipNode:setTag(imgP:getTag()+100)
imgP:getParent():addChild(clipNode)
imgP:getParent():getChildren():removeObject(imgP,false);
imgP:setParent(nil);
imgP:setTag(1)
imgP:setPosition(ccp(0,0))
clipNode:addChild(imgP);
return imgP
end