欢迎来到代码驿站!

C代码

当前位置:首页 > 软件编程 > C代码

cocos2dx实现刮奖效果

时间:2021-07-16 10:42:22|栏目:C代码|点击:

本文实例为大家分享了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"); 
} 

上一篇:C语言实现从文件读入一个3*3数组,并计算每行的平均值

栏    目:C代码

下一篇:基于C++ list中erase与remove函数的使用详解

本文标题:cocos2dx实现刮奖效果

本文地址:http://www.codeinn.net/misctech/157891.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有