cocos2d-x3.2中使用的C++11特性总结
2014-10-14 11:54
525 查看
1.auto关键字
2.std::thread
异步加载纹理可以使用 Director::getInstance()->getTextureCache()->addImageAsync
异步加载声音和处理网络请求可以使用std::thread
#include <thread>
void callfn(){
std::cout << "Hello thread! " << std::endl;
}
int main(){
std::thread* t1 = new std::thread(callfn);
t1->join();
deletet1;
t1 = nullptr;
return 0;
}
class AppDelegate : private cocos2d::Application
{
private:
std::thread *_loadingAudioThread;
void loadingAudio();
public:
};
AppDelegate::AppDelegate()
{
_loadingAudioThread = new std::thread(&AppDelegate::loadingAudio,this);
}
AppDelegate::~AppDelegate()
{
_loadingAudioThread->join();
CC_SAFE_DELETE(_loadingAudioThread);
}
void AppDelegate::loadingAudio()
{
//初始化 音乐
SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/Jazz.mp3");
SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/Synth.mp3");
//初始化 音效
SimpleAudioEngine::getInstance()->preloadEffect("sound/Blip.wav");
}
3.nullptr
4.Range for
5.Lambda函数和算法,一般在事件监听中使用可以使代码优化
= [](){};[]捕获变量列表
void
HelloWorld::onEnter()
{
Layer::onEnter();
log("HelloWorld onEnter");
// 创建一个事件监听器 OneByOne 为单点触摸
auto listener = EventListenerTouchOneByOne::create();
// 设置是否吞没事件,在 onTouchBegan 方法返回 true 时吞没
listener->setSwallowTouches(true);
// 使用 lambda 实现 onTouchBegan 事件回调函数
listener->onTouchBegan = [](Touch* touch, Event* event){
// 获取事件所绑定的 target
auto target = static_cast<Sprite*>(event->getCurrentTarget());
// 获取当前点击点所在相对按钮的位置坐标
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
// 点击范围判断检测
if (rect.containsPoint(locationInNode))
{
log("sprite x = %f, y = %f ", locationInNode.x, locationInNode.y);
log("sprite tag = %d", target->getTag());
target->runAction(ScaleBy::create(0.06f, 1.06f));
return true;
}
return false;
};
// 触摸移动时触发
listener->onTouchMoved = [](Touch* touch, Event* event){
auto target = static_cast<Sprite*>(event->getCurrentTarget());
// 移动当前按钮精灵的坐标位置
target->setPosition(target->getPosition() + touch->getDelta());
};
// 点击事件结束处理
listener->onTouchEnded = [](Touch* touch, Event* event){
auto target = static_cast<Sprite*>(event->getCurrentTarget());
log("sprite onTouchesEnded.. ");
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
// 点击范围判断检测
if (rect.containsPoint(locationInNode))
{
log("sprite x = %f, y = %f ", locationInNode.x, locationInNode.y);
log("sprite tag = %d", target->getTag());
target->runAction(ScaleTo::create(0.06f, 1.0f));
}
};
// 添加监听器
EventDispatcher* eventDispatcher = Director::getInstance()->getEventDispatcher();
eventDispatcher->addEventListenerWithSceneGraphPriority(listener, getChildByTag(kBoxA_Tag));
eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), getChildByTag(kBoxB_Tag));
eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), getChildByTag(kBoxC_Tag));
}
比较和下面处理的区别:
void
HelloWorld::onEnter()
{
Layer::onEnter();
log("HelloWorld onEnter");
// 创建一个事件监听器 OneByOne 为单点触摸
auto listener = EventListenerTouchOneByOne::create();
// 设置是否吞没事件,在 onTouchBegan 方法返回 true 时吞没
listener->setSwallowTouches(true);
// onTouchBegan 事件回调函数
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::touchBegan, this);
// onTouchMoved 事件回调函数
listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::touchMoved, this);
// onTouchEnded 事件回调函数
listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::touchEnded, this);
// 添加监听器
EventDispatcher* eventDispatcher = Director::getInstance()->getEventDispatcher();
eventDispatcher->addEventListenerWithSceneGraphPriority(listener, getChildByTag(kBoxA_Tag));
eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), getChildByTag(kBoxB_Tag));
eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), getChildByTag(kBoxC_Tag));
}
bool
HelloWorld::touchBegan(Touch* touch, Event* event)
{
return false;
}
void HelloWorld::touchMoved(Touch *touch, Event *event)
{
}
void HelloWorld::touchEnded(Touch *touch, Event *event)
{
}
2.std::thread
异步加载纹理可以使用 Director::getInstance()->getTextureCache()->addImageAsync
异步加载声音和处理网络请求可以使用std::thread
#include <thread>
void callfn(){
std::cout << "Hello thread! " << std::endl;
}
int main(){
std::thread* t1 = new std::thread(callfn);
t1->join();
deletet1;
t1 = nullptr;
return 0;
}
class AppDelegate : private cocos2d::Application
{
private:
std::thread *_loadingAudioThread;
void loadingAudio();
public:
};
AppDelegate::AppDelegate()
{
_loadingAudioThread = new std::thread(&AppDelegate::loadingAudio,this);
}
AppDelegate::~AppDelegate()
{
_loadingAudioThread->join();
CC_SAFE_DELETE(_loadingAudioThread);
}
void AppDelegate::loadingAudio()
{
//初始化 音乐
SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/Jazz.mp3");
SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/Synth.mp3");
//初始化 音效
SimpleAudioEngine::getInstance()->preloadEffect("sound/Blip.wav");
}
3.nullptr
4.Range for
5.Lambda函数和算法,一般在事件监听中使用可以使代码优化
= [](){};[]捕获变量列表
void
HelloWorld::onEnter()
{
Layer::onEnter();
log("HelloWorld onEnter");
// 创建一个事件监听器 OneByOne 为单点触摸
auto listener = EventListenerTouchOneByOne::create();
// 设置是否吞没事件,在 onTouchBegan 方法返回 true 时吞没
listener->setSwallowTouches(true);
// 使用 lambda 实现 onTouchBegan 事件回调函数
listener->onTouchBegan = [](Touch* touch, Event* event){
// 获取事件所绑定的 target
auto target = static_cast<Sprite*>(event->getCurrentTarget());
// 获取当前点击点所在相对按钮的位置坐标
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
// 点击范围判断检测
if (rect.containsPoint(locationInNode))
{
log("sprite x = %f, y = %f ", locationInNode.x, locationInNode.y);
log("sprite tag = %d", target->getTag());
target->runAction(ScaleBy::create(0.06f, 1.06f));
return true;
}
return false;
};
// 触摸移动时触发
listener->onTouchMoved = [](Touch* touch, Event* event){
auto target = static_cast<Sprite*>(event->getCurrentTarget());
// 移动当前按钮精灵的坐标位置
target->setPosition(target->getPosition() + touch->getDelta());
};
// 点击事件结束处理
listener->onTouchEnded = [](Touch* touch, Event* event){
auto target = static_cast<Sprite*>(event->getCurrentTarget());
log("sprite onTouchesEnded.. ");
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
// 点击范围判断检测
if (rect.containsPoint(locationInNode))
{
log("sprite x = %f, y = %f ", locationInNode.x, locationInNode.y);
log("sprite tag = %d", target->getTag());
target->runAction(ScaleTo::create(0.06f, 1.0f));
}
};
// 添加监听器
EventDispatcher* eventDispatcher = Director::getInstance()->getEventDispatcher();
eventDispatcher->addEventListenerWithSceneGraphPriority(listener, getChildByTag(kBoxA_Tag));
eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), getChildByTag(kBoxB_Tag));
eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), getChildByTag(kBoxC_Tag));
}
比较和下面处理的区别:
void
HelloWorld::onEnter()
{
Layer::onEnter();
log("HelloWorld onEnter");
// 创建一个事件监听器 OneByOne 为单点触摸
auto listener = EventListenerTouchOneByOne::create();
// 设置是否吞没事件,在 onTouchBegan 方法返回 true 时吞没
listener->setSwallowTouches(true);
// onTouchBegan 事件回调函数
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::touchBegan, this);
// onTouchMoved 事件回调函数
listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::touchMoved, this);
// onTouchEnded 事件回调函数
listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::touchEnded, this);
// 添加监听器
EventDispatcher* eventDispatcher = Director::getInstance()->getEventDispatcher();
eventDispatcher->addEventListenerWithSceneGraphPriority(listener, getChildByTag(kBoxA_Tag));
eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), getChildByTag(kBoxB_Tag));
eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), getChildByTag(kBoxC_Tag));
}
bool
HelloWorld::touchBegan(Touch* touch, Event* event)
{
return false;
}
void HelloWorld::touchMoved(Touch *touch, Event *event)
{
}
void HelloWorld::touchEnded(Touch *touch, Event *event)
{
}
相关文章推荐
- Cocos2d-x3.2总结---使用物理引擎进行碰撞检测
- C++11常用特性的使用经验总结
- [转载]Cocos2d-x3.2总结(四)使用物理引擎进行碰撞检测
- c++11新特性总结和boost库的使用
- Cocos2d-x3.2总结(四)使用物理引擎进行碰撞检测
- C++11常用新特性使用经验总结
- 【干货】C++11常用特性的使用经验总结
- cocos2d-x3.2总结(二)使用TexturePacker工具创建和使用纹理图集
- Cocos2d-x3.2总结:使用物理引擎进行碰撞检测
- C++primer中提到的C++11新特性总结
- C++11新特性测试和使用(9个新特征)
- c++11新特性:通过使用rvalue引用 引入 move semantic 和 perfect forwarding
- cocos2d中CCSprite的使用总结
- cocos2d-x CCSpriteFrameCache类使用总结
- C++开发者都应该使用的10个C++11特性
- libpomelo的cocos2d-x客户端使用总结
- C++开发者都应该使用的10个C++11特性(转)
- cocos2d-x v3.0新特性及使用
- cocos2d-x工程中,让xcode4.6能够使用C++11标准库
- 【C++11】新特性——auto的使用