雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

cocos2dx實現(xiàn)刮獎效果

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊:

本文實例為大家分享了cocos2dx刮獎效果實現(xiàn)代碼,供大家參考,具體內(nèi)容如下

刮獎效果其實挺簡單的,需要用到RenderTexture來進行渲染,通過你所要渲染的圖層,把該層的顏色進行設(shè)置混合就可以達到效果,具體看代碼,我用的lua實現(xiàn)的。

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); 
 -- 設(shè)置混合模式 
 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); //渲染紋理層需加入該父節(jié)點層 
 
 
 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"); 
} 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:C++關(guān)于Makefile的詳解含通用模板

欄    目:C語言

下一篇:關(guān)于C++內(nèi)部類的介紹與使用示例

本文標題:cocos2dx實現(xiàn)刮獎效果

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/587.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有