Cocos2d-x开发实例:使用Lambda 表达式
2016-11-19 13:12
671 查看
http://blog.csdn.net/tonny_guan/article/details/38149367
我们可以将下面的代码:
[html] view
plain copy
print?
listener->onTouchBegan =CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
... ...
bool HelloWorld::onTouchBegan(Touch*touch, Event* event) {
......
returnfalse;
}
替换为如下代码:
[html] view
plain copy
print?
listener->onTouchBegan = [](Touch*touch, Event* event){
... ...
return false;
};
上面的语句[](Touch* touch, Event* event){ …}就是Lambda表达式。Lambda表达式就是JavaScript语言中的匿名函数,Java中的匿名内部类,就是在表达式中直接声明函数,而不是独立声明函数。
提示 在Lambda表达式中[]表示接下来开始定义Lambda函数,[]之后的()是Lambda函数的参数列表,{}中间就是函数体。
重构之后的HelloWorldScene.cpp主要修改的代码如下:
[html] view
plain copy
print?
void HelloWorld::onEnter()
{
Layer::onEnter();
log("HelloWorldonEnter");
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [](Touch* touch, Event* event){ ①
auto target = static_cast<Sprite*>(event->getCurrentTarget());
PointlocationInNode = 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("spritetag = %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.. ");
PointlocationInNode = 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));
}
上述代码第①、②、③行分别使用了Lambda表达式定义的匿名函数,具体代码不用再解释。从上面代码看使用Lambda表达式非常简洁,由于不需要单独定义回调函数,对应的头文件代码也比较简洁,HelloWorldScene.h主要代码如下:
[html] view
plain copy
print?
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
virtualvoid onEnter();
virtualvoid onExit();
CREATE_FUNC(HelloWorld);
};
除了触摸事件还有键盘事件、鼠标事件、加速度事件和自定义事件等也都可以使用Lambda表达式。
在Cocos2d-x 3.0之后提供了对C++11标准[1]的支持,其中的Lambda[2]表达式使用起来非常简洁。我们可以使用Lambda表达式重构上一节的实例。
我们可以将下面的代码:[html] view
plain copy
print?
listener->onTouchBegan =CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
... ...
bool HelloWorld::onTouchBegan(Touch*touch, Event* event) {
......
returnfalse;
}
替换为如下代码:
[html] view
plain copy
print?
listener->onTouchBegan = [](Touch*touch, Event* event){
... ...
return false;
};
上面的语句[](Touch* touch, Event* event){ …}就是Lambda表达式。Lambda表达式就是JavaScript语言中的匿名函数,Java中的匿名内部类,就是在表达式中直接声明函数,而不是独立声明函数。
提示 在Lambda表达式中[]表示接下来开始定义Lambda函数,[]之后的()是Lambda函数的参数列表,{}中间就是函数体。
重构之后的HelloWorldScene.cpp主要修改的代码如下:
[html] view
plain copy
print?
void HelloWorld::onEnter()
{
Layer::onEnter();
log("HelloWorldonEnter");
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [](Touch* touch, Event* event){ ①
auto target = static_cast<Sprite*>(event->getCurrentTarget());
PointlocationInNode = 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("spritetag = %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.. ");
PointlocationInNode = 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));
}
上述代码第①、②、③行分别使用了Lambda表达式定义的匿名函数,具体代码不用再解释。从上面代码看使用Lambda表达式非常简洁,由于不需要单独定义回调函数,对应的头文件代码也比较简洁,HelloWorldScene.h主要代码如下:
[html] view
plain copy
print?
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
virtualvoid onEnter();
virtualvoid onExit();
CREATE_FUNC(HelloWorld);
};
除了触摸事件还有键盘事件、鼠标事件、加速度事件和自定义事件等也都可以使用Lambda表达式。
相关文章推荐
- Cocos2d-x开发实例:使用Lambda 表达式
- Cocos2d-x开发实例:使用Lambda 表达式
- Cocos2d-x UI开发之CCControlSwitch控件类使用实例
- 实例讲解C++编程中lambda表达式的使用
- Cocos2d-x UI开发之文本类使用实例
- Cocos2d-x v3.0 lambda表达式的使用
- Java 8 之 lambda 表达式简单使用入门实例代码。
- Cocos2d-x UI开发之CCControlSlider控件类使用实例
- 【Python开发】Lambda表达式使用
- Cocos2d-x 3.2 lua飞机大战开发实例(一)背景地图,飞机,子弹的封装,以及lua中定时器的使用
- Cocos2d-x开发实例介绍帧动画使用
- Cocos2d-x UI开发之CCControlColourPicker控件类使用实例
- Cocos2d-x之lambda表达式的使用
- Android 开发使用 Java 8 中Lambda 表达式功能
- Cocos2d-x v3.0 lambda表达式的使用
- Cocos2d-x UI开发之CCControlPotentiometer控件类使用实例
- Cocos2d-x UI开发之菜单类使用实例
- Java 8 之 lambda 表达式简单使用入门实例代码
- Cocos2d-x开发实例介绍帧动画使用
- Android 中Lambda表达式的使用实例详解