cocos2d-X之蒙板,局部高亮可点,CCRenderTexture

本文介绍如何在Cocos2d-X中创建一个局部高亮、可点击的蒙版,用于游戏新手引导。通过使用CCLayerColor、CCRenderTexture和自定义精灵实现动态调整高亮区域,避免每步都需要新的美术资源。通过继承CCTargetedTouchDelegate处理触摸事件,确保高亮区域的点击优先级。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

蒙板,局部高亮可点的用处大多是在新手引导的时候,引导玩家一步一步的走游戏的操作流程。

因为设置成圆角的,比较美观,如果不用圆角,可以直接把Sprite改成layercolor,这个版本的缺点是每步都需要美术出一个资源(如果每步的点击区域大小不一样的话),现在这个版本是直接设置大小即可。

这个版本的设计思路是,用layercolor,先把高亮的区域设置好了,然后在layercolor的四个角加上一个小圆角的sprite,这样就不用每步都需要美术出资源了。可以随意的设置高亮区域的大小。本来想直接不用sprite,直接找个方法设置layercolor的圆角的,没找到合适的方法,如果大家有比较合适的更简便的方法,欢迎拍砖。


原理是:渲染的时候,使用异步混合渲染。ccBlendFunc

不多说,直接上代码

为了获取点击事件,所以继承了 CCTargetedTouchDelegate,把touch的权限设置为最高。

//.h  

[cpp]  view plain copy
  1. #ifndef Good_PlayGuide_h  
  2. #define Good_PlayGuide_h  
  3.   
  4. #include "cocos2d.h"  
  5.   
  6. using namespace cocos2d;  
  7.   
  8. class PlayerGuide:public CCSprite , public CCTargetedTouchDelegate  
  9. {  
  10. public:  
  11.     PlayerGuide();  
  12.     virtual ~PlayerGuide();  
  13.       
  14.     virtual bool init();  
  15.     virtual void onEnter();  
  16.     virtual void onExit();  
  17.       
  18.     virtual bool ccTouchBegan(CCTouch *touch, CCEvent *event);  
  19.     virtual void ccTouchMoved(CCTouch *touch, CCEvent *event);  
  20.     virtual void ccTouchEnded(CCTouch *touch, CCEvent *event);  
  21.       
  22.     void onSetSpriteAndPosition(float width,float height,CCPoint point);  
  23.     CCLayerColor *m_layer; // color layer  
  24.     float m_layerWidth; //layer width   
  25.     float m_layerHeight; // layer height   
  26.     CCRenderTexture *m_pTarget; // render texture   
  27. };  
  28. #endif  


[cpp]  view plain copy
  1. #include <iostream>  
  2. #include "PlayerGuide.h"  
  3.   
  4.   
  5. PlayerGuide::PlayerGuide()  
  6. {  
  7.     m_layerWidth = 0.0f;  
  8.     m_layerHeight = 0.0f;  
  9. }  
  10.   
  11. PlayerGuide::~PlayerGuide()  
  12. {  
  13.      
  14. }  
  15.   
  16. bool PlayerGuide::init()  
  17. {  
  18.     if (!CCSprite::init()) {  
  19.         return false;  
  20.     }  
  21.       
  22.     return true;  
  23. }  
  24.   
  25. void PlayerGuide::onEnter()  
  26. {  
  27.     //since v2.0  
  28. //    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -201, true);  
  29. //当进入这个对象时,设置touch事件的响应权限,menu的响应级别是-128,我们要获取比这个要高的权限,(设置的数值越低,权限越高)  
  30.     CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, -201, true);  
  31.     CCSprite::onEnter();  
  32. }  
  33.   
  34. void PlayerGuide::onExit()  
  35. {  
  36.     //since v2.0  
  37. //    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);  
  38. //退出时,把touch响应的事件 移除  
  39.     CCTouchDispatcher::sharedDispatcher()->removeDelegate(this);  
  40.     CCSprite::onExit();  
  41. }  
  42. #pragma mark  --------------------------setSpriteAndPosition----------------------------  
  43.   
  44. /******************************************************************************* 
  45. *@param width 设置高亮区域的宽度,height 高亮区域的高度,point 设置layer的position 
  46. * 
  47. * 
  48. *******************************************************************************/  
  49.   
  50. void PlayerGuide::onSetSpriteAndPosition(float width,float height, CCPoint point)  
  51. {  
  52.     if (width != 0 && height != 0) {  
  53.         m_layerWidth = width;  
  54.         m_layerHeight = height;  
  55.         m_layer = CCLayerColor::layerWithColorWidthHeight(ccc4(0x00, 0x00, 0x00, 0xff), width, height);  
  56.         m_layer->retain();  
  57.         ccBlendFunc ccb = {GL_ZERO,GL_ONE_MINUS_SRC_ALPHA};  
  58.           
  59.         m_layer->setBlendFunc(ccb);  
  60.         m_layer->setPosition(point);  
  61.           
  62.         CCSprite *topright = CCSprite::spriteWithFile("yourfile");//自己设置圆角的小图,如果不需要圆角,可以直接把sprite去掉  
  63.         topright->getTexture()->setAliasTexParameters();  
  64.         ccBlendFunc cbf = {GL_ONE,GL_ONE_MINUS_DST_ALPHA};  
  65.         topright->setBlendFunc(cbf);  
  66.         topright->setPosition(ccp(m_layer->getContentSize().width - topright->getContentSize().width/2,m_layer->getContentSize().height - topright->getContentSize().height/2));  
  67.         topright->setOpacity(255*0.5);  
  68.         m_layer->addChild(topright,1);  
  69.           
  70.         CCSprite *topleft = CCSprite::spriteWithFile(IMG_PATH(IMAGE_PLAYERGUIDE_ROUNDED));  
  71.         topleft->getTexture()->setAliasTexParameters();  
  72.         ccBlendFunc cbf1 = {GL_ONE,GL_ONE_MINUS_DST_ALPHA};  
  73.         topleft->setBlendFunc(cbf1);  
  74.         topleft->setPosition(ccp(topleft->getContentSize().width/2,m_layer->getContentSize().height - topleft->getContentSize().height/2));  
  75.         topleft->setOpacity(255*0.5);  
  76.         topleft->setFlipX(true);  
  77.         m_layer->addChild(topleft,1);  
  78.           
  79.         CCSprite *buttomleft = CCSprite::spriteWithFile(IMG_PATH(IMAGE_PLAYERGUIDE_ROUNDED));  
  80.         buttomleft->getTexture()->setAliasTexParameters();  
  81.         ccBlendFunc cbf2 = {GL_ONE,GL_ONE_MINUS_DST_ALPHA};  
  82.         buttomleft->setBlendFunc(cbf2);  
  83.         buttomleft->setPosition(ccp(buttomleft->getContentSize().width/2,buttomleft->getContentSize().height/2));  
  84.         buttomleft->setOpacity(255*0.5);  
  85.         buttomleft->setFlipX(true);  
  86.         buttomleft->setFlipY(true);  
  87.         m_layer->addChild(buttomleft,1);  
  88.           
  89.         CCSprite *buttomright = CCSprite::spriteWithFile(IMG_PATH(IMAGE_PLAYERGUIDE_ROUNDED));  
  90.         buttomright->getTexture()->setAliasTexParameters();  
  91.         ccBlendFunc cbf3 = {GL_ONE,GL_ONE_MINUS_DST_ALPHA};  
  92.         buttomright->setBlendFunc(cbf3);  
  93.         buttomright->setPosition(ccp(m_layer->getContentSize().width - buttomleft->getContentSize().width/2,buttomright->getContentSize().height/2));  
  94.         buttomright->setOpacity(255*0.5);  
  95.         buttomright->setFlipY(true);  
  96.         m_layer->addChild(buttomright,1);  
  97.     }  
  98.       
  99.     CCSize s = CCDirector::sharedDirector()->getWinSize();  
  100.       
  101.     m_pTarget = CCRenderTexture::renderTextureWithWidthAndHeight(s.width, s.height);  
  102.     ccBlendFunc ccb1 = {GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA};  
  103.     m_pTarget->getSprite()->setBlendFunc(ccb1);  
  104.     m_pTarget->clear(0.0f, 0.0f, 0.0f, 0.5f);  
  105.       
  106.     m_pTarget->setPosition(ccp(s.width/2,s.height/2));  
  107.       
  108.     this->addChild(m_pTarget);  
  109.       
  110.     m_pTarget->begin();  
  111.     if (width != 0 && height != 0) {  
  112.          m_layer->visit();  
  113.     }  
  114.      
  115.     m_pTarget->end();  
  116. }  
  117.   
  118. bool PlayerGuide::ccTouchBegan(cocos2d::CCTouch *touch, cocos2d::CCEvent *event)  
  119. {  
  120.     CCPoint touchpoint = touch->locationInView(touch->view());  
  121.     touchpoint = CCDirector::sharedDirector()->convertToGL(touchpoint);  
  122.   
  123. if (m_layerWidth != 0 && m_layerHeight != 0) {  
  124.     //如果点击高亮区域,则响应下层的区域  
  125.             CCRect rect = m_layer->boundingBox();  
  126.             if (CCRect::CCRectContainsPoint(rect, touchpoint)) {  
  127.                 return false;  
  128.             }  
  129.   
  130.       
  131.     return true;  
  132. }  
  133.   
  134. void PlayerGuide::ccTouchMoved(cocos2d::CCTouch *touch, cocos2d::CCEvent *event)  
  135. {  
  136.       
  137. }  
  138.   
  139. void PlayerGuide::ccTouchEnded(cocos2d::CCTouch *touch, cocos2d::CCEvent *event)  
  140. {  
  141.       
  142. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值