cocos2d-x 显示触摸操作(显示水波点击效果,用于视频演示)-性能改造

http://blog.csdn.net/hitwhylz/article/details/26042751

首先是显示触摸操作在文章最后,对性能进行一些提升改造。

因为要演示我们的作品。使用试玩过程中, 如果没办法显示我们的触摸操作(像录制视频一样, 点击了屏幕某点, 出现红点或者水波荡漾这样的效果), 那样的话演示效果不好。观众就无法直观的了解我们的游戏。所以考虑加入这个功能。

之后, 走了点弯路。一直在考虑手机本身有没有这个功能,后来找了很久。非越狱iPhone是没有这个功能的。

于是乎, 自己写呗。

具体效果如下:



实现很简单,主要用到了一个粒子效果。

具体步骤如下:

0.导入粒子效果文件. showClick.png + showClick.plist(可在我给出的demo中下载)

1.开启触摸

2.在ccTouchBegan中获取触摸点

3.在该触摸点中添加粒子效果


好了。下面给出具体代码。

当然, 也可以去我的Github中下载源码:

https://github.com/colin1994/showClickTest


代码如下:(注意:在头文件添加 USING_NS_CC;亦可但是必须添加)

HelloWorld.h

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #ifndef __HELLOWORLD_SCENE_H__  
  2. #define __HELLOWORLD_SCENE_H__  
  3.   
  4. #include "cocos2d.h"  
  5. using namespace cocos2d;  
  6.   
  7. class HelloWorld : public cocos2d::CCLayer  
  8. {  
  9. public:  
  10.     // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)  
  11.     virtual bool init();  
  12.   
  13.     // there's no 'id' in cpp, so we recommend to return the class instance pointer  
  14.     static cocos2d::CCScene* scene();  
  15.       
  16.     // a selector callback  
  17.     void menuCloseCallback(CCObject* pSender);  
  18.   
  19.     // preprocessor macro for "static create()" constructor ( node() deprecated )  
  20.     CREATE_FUNC(HelloWorld);  
  21.       
  22.       
  23.     //进入, 退出响应  
  24.     virtual void onEnter();  
  25.     virtual void onExit();  
  26.       
  27.     //触屏逻辑函数  
  28.     virtual void registerWithTouchDispatcher(void);  
  29.     virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);  
  30. };  
  31.   
  32. #endif // __HELLOWORLD_SCENE_H__  

HelloWorld.m

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "HelloWorldScene.h"  
  2. #include "SimpleAudioEngine.h"  
  3.   
  4. using namespace cocos2d;  
  5. using namespace CocosDenshion;  
  6.   
  7. CCScene* HelloWorld::scene()  
  8. {  
  9.     // 'scene' is an autorelease object  
  10.     CCScene *scene = CCScene::create();  
  11.       
  12.     // 'layer' is an autorelease object  
  13.     HelloWorld *layer = HelloWorld::create();  
  14.   
  15.     // add layer as a child to scene  
  16.     scene->addChild(layer);  
  17.   
  18.     // return the scene  
  19.     return scene;  
  20. }  
  21.   
  22.   
  23. // on "init" you need to initialize your instance  
  24. bool HelloWorld::init()  
  25. {  
  26.     //  
  27.     // 1. super init first  
  28.     if ( !CCLayer::init() )  
  29.     {  
  30.         return false;  
  31.     }  
  32.   
  33.       
  34.     return true;  
  35. }  
  36.   
  37. void HelloWorld::menuCloseCallback(CCObject* pSender)  
  38. {  
  39.     CCDirector::sharedDirector()->end();  
  40.   
  41. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)  
  42.     exit(0);  
  43. #endif  
  44. }  
  45.   
  46.   
  47. #pragma mark - enter,exit  
  48. //进入响应函数  
  49. void HelloWorld::onEnter()  
  50. {  
  51.     CCLayer::onEnter();  
  52.     //进入开启触摸  
  53.     this->setTouchEnabled(true);  
  54. }  
  55. //退出响应函数  
  56. void HelloWorld::onExit()  
  57. {  
  58.     CCLayer::onExit();  
  59. }  
  60.   
  61. #pragma mark - 触摸事件  
  62.   
  63. void HelloWorld::registerWithTouchDispatcher()  
  64. {  
  65.     //kCCMenuHandlerPriority=-128,将这个值设置为-128的二倍,可以比下边的层的优先级高  
  66.     //而且ccTouchBegan的返回值为true,说明其他的层将接受不到这个触摸消息了,只有这个层上边的  
  67.     //菜单的优先级比他还要打,所以它上边的菜单是可以接收到触摸消息的  
  68.     CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,  
  69.                                                                             kCCMenuHandlerPriority*2,true);  
  70. }  
  71. //触摸事件  
  72. bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)  
  73. {  
  74.     //获得触摸点坐标  
  75.     CCPoint touchLocation = pTouch->getLocation();  
  76.       
  77.     CCParticleSystemQuad *mParticle =  CCParticleSystemQuad::create("showClick.plist");  
  78.     mParticle->setScale(0.5f);  
  79.     mParticle->setPosition(touchLocation);  
  80.     //如果不设置,粒子播放后内存不释放  
  81.     mParticle->setAutoRemoveOnFinish(true);  
  82.     this->addChild(mParticle);  
  83.       
  84.     return false;  
  85. }  

=============
2次改造性能提升
ParticleBatchNode可以引用且只可以引用1个texture(一个图片文件,一个texture图集),增加到SpriteBatchNode中的ParticleSystem都是在OpenGL ES调用绘图函数时绘制的。
 
如果ParticleSystem没有增加到ParticleBatchNode中,OpenGL ES会调用每个粒子系统的绘图函数,这样做效率会比较低。
bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)  
{  
    //获得触摸点坐标  
    CCPoint touchLocation = pTouch->getLocation();  
      
    CCParticleSystemQuad *mParticle =  CCParticleSystemQuad::create("showClick.plist"); 


    mParticle->setScale(0.5f);  
    mParticle->setPosition(touchLocation);  
//添加ParticleBatchNode 
mParticle->retain();
CCParticleBatchNode *batch = CCParticleBatchNode::createWithTexture(mParticle->getTexture()); 

batch->addChild(mParticle);
this->addChild(batch);
    mParticle->release();


    return false;  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值