[Quick-x]制作新手引导高亮区域方法之二:裁剪模式

demo下载:https://github.com/chenquanjun/Quick-x-HighlightArea

2、裁剪模式

(1)创建裁剪对象

    local bgColor = ccc3(255, 0, 0) --非高亮区域颜色
    local bgOpacity = 0.6 --非高亮区域透明度

    local layerColor = CCLayerColor:create(ccc4(bgColor.r, bgColor.g, bgColor.b, bgOpacity * 255), size.width, size.height)

    local clipNode = CCClippingNode:create();
    clipNode:setInverted(true)--设定遮罩的模式true显示没有被遮起来的纹理   如果是false就显示遮罩起来的纹理  
    clipNode:setAlphaThreshold(0) --设定遮罩图层的透明度取值范围 
    clipNode:addChild(layerColor)
    self:addChild(clipNode)

(2)创建用来裁剪的对象

因为这里都是使用同一张贴图,所以使用CCSpriteBatchNode统一创建

    local batchNode = CCSpriteBatchNode:create("Images/circle.png")

    local circleSpr = CCSprite:createWithTexture(batchNode:getTexture())
    local circleSize = circleSpr:getContentSize()

    local widthPara 
    local heightPara

    local rectArray = {
                         [1] = CCRect(100, 100, 100, 100),
                         [2] = CCRect(200, 100, 100, 150),
                         [3] = CCRect(450, 35, 150, 100),
                         [4] = CCRect(300, 300, 100, 100),
                        }

    for i, rect in ipairs(rectArray) do
        local circleSpr = CCSprite:createWithTexture(batchNode:getTexture())

        if not widthPara then
            local circleSize = circleSpr:getContentSize()

            --宽度和高度参数,1.4142为根号2,矩形的外接椭圆的长轴与短轴长度
            widthPara = 1.4142 / circleSize.width
            heightPara = 1.4142 / circleSize.height
        end

        local fScaleX = widthPara * rect.size.width
        local fScaleY = heightPara * rect.size.height
        circleSpr:setScaleX(fScaleX)
        circleSpr:setScaleY(fScaleY)
        circleSpr:setPosition(rect:getMidX(), rect:getMidY())
        batchNode:addChild(circleSpr)
    end
 
    clipNode:setStencil(batchNode) --关键代码

具体效果(裁剪模板模式 StencilMode)

 

教程方法一的使用的混合模式(BlendMode)

 

原始图片(Origin)

 

可以看到,裁剪模式已经出现了锯齿(后面会用另外一种方式解决这个问题)

 

demo下载:https://github.com/chenquanjun/Quick-x-HighlightArea

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值