记录一下触摸事件的创建与使用
auto listener = EventListenerTouchOneByOne::create();//创建一个监听器
listener->setSwallowTouches(true);//不向下传递触摸
//触摸开始
listener->onTouchBegan = [=](Touch* touch, Event* event) {
log("onTouchBegan");
//获取触摸目标
auto target = dynamic_cast<Sprite*>(event->getCurrentTarget());
//获取触摸点
Point locationInNode = target->convertToNodeSpace(touch->getLocation());
//获取目标的轮廓大小
Size s = target->getContentSize();
//区域位置(x,y,width,height)
Rect rect = Rect(0, 0, s.width, s.height);
//判断触摸点是否在目标轮廓区域内
if (rect.containsPoint(locationInNode)) {
auto moveToAction = MoveTo::create(1, Vec2(150, 200));
_skill_1->runAction(moveToAction);
return true;//在,返回true
}
else {
return false;//不在,返回false
}
};
//触摸移动
listener->onTouchMoved = [](Touch* touch, Event* event) {
log("onTouchMoved");
};
//触摸结束
listener->onTouchEnded = [=](Touch* touch, Event* event) {
log("onTouchEnded");
auto moveToAction = MoveTo::create(1, Vec2(150, 120));
_skill_1->runAction(moveToAction);
};
//触摸取消
listener->onTouchCancelled = [](Touch* touch, Event* event) {
log("onTouchEnded");
};
//最后将精灵添加到事件监听器中
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, _skill_1);