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, 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)
{
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值