cocos2d-x 显示触摸操作(单击显示效果浪潮,对于视频演示)-绩效转型
2015-07-05 13:31
736 查看
http://blog.csdn.net/hitwhylz/article/details/26042751
首先是显示触摸操作。
在文章最后。对性能进行一些提升改造。
由于要演示我们的作品。使用试玩过程中, 假设没办法显示我们的触摸操作(像录制视频一样, 点击了屏幕某点, 出现红点或者水波荡漾这种效果), 那样的话演示效果不好。观众就无法直观的了解我们的游戏。
所以考虑增加这个功能。
之后, 走了点弯路。一直在考虑手机本身有没有这个功能。后来找了非常久。
非越狱iPhone是没有这个功能的。
于是乎, 自己写呗。
详细效果例如以下:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201507/58bba2822239dd3387d63b7449bf54ec)
实现非常easy,主要用到了一个粒子效果。
详细过程例如以下:
0.导入粒子效果文件. showClick.png + showClick.plist(可在我给出的demo中下载)
1.开启触摸
2.在ccTouchBegan中获取触摸点
3.在该触摸点中加入粒子效果
好了。
以下给出详细代码。
当然, 也能够去我的Github中下载源代码:
https://github.com/colin1994/showClickTest
代码例如以下:(注意:在头文件加入 USING_NS_CC;亦可可是必须加入)
HelloWorld.h
[cpp] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
using namespace cocos2d;
class HelloWorld : public cocos2d::CCLayer
{
public:
// Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
virtual bool init();
// there's no 'id' in cpp, so we recommend to return the class instance pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// preprocessor macro for "static create()" constructor ( node() deprecated )
CREATE_FUNC(HelloWorld);
//进入, 退出响应
virtual void onEnter();
virtual void onExit();
//触屏逻辑函数
virtual void registerWithTouchDispatcher(void);
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorld.m
[cpp] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d;
using namespace CocosDenshion;
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
return true;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
#pragma mark - enter,exit
//进入响应函数
void HelloWorld::onEnter()
{
CCLayer::onEnter();
//进入开启触摸
this->setTouchEnabled(true);
}
//退出响应函数
void HelloWorld::onExit()
{
CCLayer::onExit();
}
#pragma mark - 触摸事件
void HelloWorld::registerWithTouchDispatcher()
{
//kCCMenuHandlerPriority=-128,将这个值设置为-128的二倍。能够比下边的层的优先级高
//并且ccTouchBegan的返回值为true,说明其它的层将接受不到这个触摸消息了,仅仅有这个层上边的
//菜单的优先级比他还要打,所以它上边的菜单是能够接收到触摸消息的
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,
kCCMenuHandlerPriority*2,true);
}
//触摸事件
bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
//获得触摸点坐标
CCPoint touchLocation = pTouch->getLocation();
CCParticleSystemQuad *mParticle = CCParticleSystemQuad::create("showClick.plist");
mParticle->setScale(0.5f);
mParticle->setPosition(touchLocation);
//假设不设置,粒子播放后内存不释放
mParticle->setAutoRemoveOnFinish(true);
this->addChild(mParticle);
return false;
}
=============
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;
}
首先是显示触摸操作。
在文章最后。对性能进行一些提升改造。
由于要演示我们的作品。使用试玩过程中, 假设没办法显示我们的触摸操作(像录制视频一样, 点击了屏幕某点, 出现红点或者水波荡漾这种效果), 那样的话演示效果不好。观众就无法直观的了解我们的游戏。
所以考虑增加这个功能。
之后, 走了点弯路。一直在考虑手机本身有没有这个功能。后来找了非常久。
非越狱iPhone是没有这个功能的。
于是乎, 自己写呗。
详细效果例如以下:
实现非常easy,主要用到了一个粒子效果。
详细过程例如以下:
0.导入粒子效果文件. showClick.png + showClick.plist(可在我给出的demo中下载)
1.开启触摸
2.在ccTouchBegan中获取触摸点
3.在该触摸点中加入粒子效果
好了。
以下给出详细代码。
当然, 也能够去我的Github中下载源代码:
https://github.com/colin1994/showClickTest
代码例如以下:(注意:在头文件加入 USING_NS_CC;亦可可是必须加入)
HelloWorld.h
[cpp] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
using namespace cocos2d;
class HelloWorld : public cocos2d::CCLayer
{
public:
// Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
virtual bool init();
// there's no 'id' in cpp, so we recommend to return the class instance pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// preprocessor macro for "static create()" constructor ( node() deprecated )
CREATE_FUNC(HelloWorld);
//进入, 退出响应
virtual void onEnter();
virtual void onExit();
//触屏逻辑函数
virtual void registerWithTouchDispatcher(void);
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorld.m
[cpp] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d;
using namespace CocosDenshion;
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
return true;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
#pragma mark - enter,exit
//进入响应函数
void HelloWorld::onEnter()
{
CCLayer::onEnter();
//进入开启触摸
this->setTouchEnabled(true);
}
//退出响应函数
void HelloWorld::onExit()
{
CCLayer::onExit();
}
#pragma mark - 触摸事件
void HelloWorld::registerWithTouchDispatcher()
{
//kCCMenuHandlerPriority=-128,将这个值设置为-128的二倍。能够比下边的层的优先级高
//并且ccTouchBegan的返回值为true,说明其它的层将接受不到这个触摸消息了,仅仅有这个层上边的
//菜单的优先级比他还要打,所以它上边的菜单是能够接收到触摸消息的
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,
kCCMenuHandlerPriority*2,true);
}
//触摸事件
bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
//获得触摸点坐标
CCPoint touchLocation = pTouch->getLocation();
CCParticleSystemQuad *mParticle = CCParticleSystemQuad::create("showClick.plist");
mParticle->setScale(0.5f);
mParticle->setPosition(touchLocation);
//假设不设置,粒子播放后内存不释放
mParticle->setAutoRemoveOnFinish(true);
this->addChild(mParticle);
return false;
}
=============
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;
}
相关文章推荐
- Cocos2d-x学习笔记(1)
- 1.cocos2dx 3.2环境结构
- cocos2d-x学习记录C++,环境搭建,创建第一个工程
- quick-cocos2d-x 中自带物理引擎 PhysicsMaterial 恢复力的问题
- win7 64位 搭建cocos2dx的环境
- 【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第二十三课:Cocos2D-X音频系统1-3
- [iuud8]基于cocos2dx2.26在mac下编译通过的.so文件移植到win32位环境
- Cocos2d-3x:vs2012项目开关android项目需要注意的地方
- Cocos2d-x制作跳棋第二步:工程建立与界面编写
- cocos2dx clippingNode的实现原理
- cocos2dx v3环境配置
- 四、cocos2d-x的弹窗弹性动画
- 《红孩儿引擎内功心法修练与Cocos2d-x》之结点系统(场景,层,精灵)
- cocos2d-x图片异步加载
- Cocos2d-x 的内存管理
- cocos2d-x 疑难奇葩错误
- 【Cocos2d-x 游戏开发】 1 --- Hello World
- Cocos2d-x 单点触摸--让我们用手指动起来的精灵
- 【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第二十二课:Cocos2D-X地图系统1-8
- Cocos2d-x 3.0final 终结者系列教程14-L新abel-Cocos2d-x公文