本文实例为大家分享了cocos2dx刮奖效果实现代码,供大家参考,具体内容如下 刮奖效果其实挺简单的,需要用到RenderTexture来进行渲染,通过你所要渲染的图层,把该层的颜色进行设置混
本文实例为大家分享了cocos2dx刮奖效果实现代码,供大家参考,具体内容如下
刮奖效果其实挺简单的,需要用到RenderTexture来进行渲染,通过你所要渲染的图层,把该层的颜色进行设置混合就可以达到效果,具体看代码,我用的lua实现的。
local winsize = cc.Director:sharedDirector():getWinSize(); local dataSprite = cc.Sprite:create("Star.png")--要把这个图片刮出来 dataSprite:setAnchorPoint(cc.p(0.5, 0.5)); dataSprite:move(winsize.width / 2.0, winsize.height / 2.0); self:addChild(dataSprite) pEarse = cc.DrawNode:create() pEarse:drawDot(cc.p(0, 0), 5, cc.c4f(1, 0, 0, 1)); pEarse:retain() pRTex = cc.RenderTexture:create(winsize.width, winsize.height); pRTex:setPosition(cc.p(winsize.width / 2, winsize.height / 2)); --this:addChild(pRTex); pRTex:retain() local pBg = cc.Sprite:create("d1.png");--这个作为当“油漆层” pBg:setAnchorPoint(cc.p(0.5, 0.5)); pBg:move(winsize.width / 2.0, winsize.height / 2.0); pRTex:begin(); dataSprite:visit(); pBg:visit(); pRTex:endToLua(); local layer=cc.Layer:create() self:addChild(layer, 1000) layer:addChild(pRTex); layer:setNodeTouch(handler(self, self.onTouchStart))
鼠标移动代码:
function shop.erasure(event) -- body print("erasure: ", event.name) --todo print("moved") local touchPoint = event.pos pEarse:setPosition(event.pos.x, event.pos.y); -- 设置混合模式 local blendFunc = { GL_ONE, GL_ZERO }; pEarse:setBlendFunc(blendFunc); -- 将橡皮擦的像素渲染到画布上,与原来的像素进行混合 pRTex:begin(); pEarse:visit(); pRTex:endToLua(); end
C++代码:
void function() { //test code auto aPanelSprite = Sprite::create("potentiometerTrack.png"); aPanelSprite->setPosition(Vec2(s.width / 2, s.height / 2)); this->addChild(aPanelSprite); pEase = DrawNode::create(); pEase->retain(); pEase->drawDot(Point(0, 0), 4.0f, Color4F(255, 0, 0, 255)); pRender = RenderTexture::create(s.width, s.height); pRender->retain(); pRender->setPosition(Vec2(s.width / 2, s.height / 2)); this->addChild(pRender); //渲染纹理层需加入该父节点层 auto pBg = Sprite::create("potentiometerProgress.png"); //这个作为当“油漆层” pBg->setAnchorPoint(Point(0.5, 0.5)); pBg->setPosition(Vec2(s.width / 2, s.height / 2)); pRender->begin(); aPanelSprite->visit(); pBg->visit(); pRender->end(); auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(SpriteEaseBezier::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(SpriteEaseBezier::onTouchMoved, this); auto _eventDispatcher = CCDirector::getInstance()->getEventDispatcher(); _eventDispatcher->addEventListenerWithFixedPriority(listener, -10); } bool SpriteEaseBezier::onTouchBegan(Touch *touch, Event *unused_event) { CCLOG("SpriteEaseBezier::onTouchBegan"); return true; } void SpriteEaseBezier::onTouchMoved(Touch *touch, Event *unused_event) { auto touchPoint = touch->getLocation(); pEase->setPosition(touchPoint.x, touchPoint.y); BlendFunc blendFunc = { GL_ONE, GL_ZERO }; pEase->setBlendFunc(blendFunc); pRender->begin(); pEase->visit(); pRender->end(); CCLOG("SpriteEaseBezier::onTouchMoved"); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。