本篇文章给大家分享的是有关使用cocos2dx怎么实现刮奖效果,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
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"); }
以上就是使用cocos2dx怎么实现刮奖效果,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。