cocos2d-x3.2中使用的C++11特性总结

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,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,s.height);
// 点击范围判断检测
if (rect.containsPoint(locationInNode))
{
log("sprite x = %f,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(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) { }

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...