您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關使用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。