
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) { }
总结以上是内存溢出为你收集整理的cocos2d-x3.2中使用的C++11特性总结全部内容,希望文章能够帮你解决cocos2d-x3.2中使用的C++11特性总结所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)