bool HelloWorld::init(),添加内容如下:
//获取可见区域的大小
Size visibleSize = Director::getInstance()->getVisibleSize();
//定义一个label,这里使用了c++11中的auto自动匹配
auto label = LabelTTF::create("bobo", "Courier", 30);
//设置label在屏幕的中间
label->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
addChild(label);//将label添加进去
//创建事件监听器
auto listener = EventListenerTouchOneByOne::create();
//设置listener
listener->onTouchBegan = [label](Touch *t, Event *e){
//判断点击的是否是label
if (label->getBoundingBox().containsPoint(t->getLocation())) {
//每个动作都有后缀为to或者by
//to是指移动到特定的位置
//by是根据当前状态继续执行,偏移等动作
//设置动作,moveto(运行时间,运行到的位置)
// label->runAction(MoveTo::create(1, Point(100,100)));
// label->runAction(MoveBy::create(1, Point(30,30)));
label->runAction(MoveBy::create(1, Point(-10, -10)));
}
return false;
};
//添加事件监听器
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, label);