bool MyTouchEvent2::init()
{
if (!Layer::init())
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
/创建3个文本标签
Label * text1 = Label::create("", "Arial", 24);
text1->setPosition(Point(400, 280));
this->addChild(text1,1,1);
Label * text2 = Label::create("", "Arial", 24);
text2->setPosition(Point(400, 200));
this->addChild(text2,1,2);
Label * text3 = Label::create("", "Arial", 24);
text3->setPosition(Point(400, 100));
this->addChild(text3,1,3);
//创建多点触摸监听器
auto listener = EventListenerTouchAllAtOnce::create();
//单击
listener->onTouchesBegan = [&](const std::vector<Touch*>& touchs, Event* event)
{
auto text = (Label *)this->getChildByTag(1);
int num = touchs.size();
text->setString(Value(num).asString() + "Touches:");
{
auto text = (Label *)this->getChildByTag(2);
int num = touchs.size();
std::string str = Value(num).asString() + "Touches:";
for (auto & touch:touchs)
{
auto location = touch->getLocation();
str += "[touchID" + Value(touch->getID()).asString() + "]";
}
text->setString(str);
{
auto text = (Label*)this->getChildByTag(3);
int num = touchs.size();
text->setString(Value(num).asString()+"Touches:");
};
//注册监听事件
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
{
if (!Layer::init())
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
/创建3个文本标签
Label * text1 = Label::create("", "Arial", 24);
text1->setPosition(Point(400, 280));
this->addChild(text1,1,1);
Label * text2 = Label::create("", "Arial", 24);
text2->setPosition(Point(400, 200));
this->addChild(text2,1,2);
Label * text3 = Label::create("", "Arial", 24);
text3->setPosition(Point(400, 100));
this->addChild(text3,1,3);
//创建多点触摸监听器
auto listener = EventListenerTouchAllAtOnce::create();
//单击
listener->onTouchesBegan = [&](const std::vector<Touch*>& touchs, Event* event)
{
auto text = (Label *)this->getChildByTag(1);
int num = touchs.size();
text->setString(Value(num).asString() + "Touches:");
};
// 移动
listener->onTouchesMoved = [&](const std::vector<Touch*>& touchs, Event* event){
auto text = (Label *)this->getChildByTag(2);
int num = touchs.size();
std::string str = Value(num).asString() + "Touches:";
for (auto & touch:touchs)
{
auto location = touch->getLocation();
str += "[touchID" + Value(touch->getID()).asString() + "]";
}
text->setString(str);
};
// 松开
listener->onTouchesEnded = [&](const std::vector<Touch*>& touchs, Event* event){
auto text = (Label*)this->getChildByTag(3);
int num = touchs.size();
text->setString(Value(num).asString()+"Touches:");
};
//注册监听事件
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
本文详细介绍了在Cocos2dx中实现多点触摸的步骤和关键代码,包括如何监听触摸事件,处理同时发生的多个触摸点,以及如何进行移动和松开操作的响应。通过实例展示,帮助开发者理解并掌握Cocos2dx的多点触摸功能。
4256

被折叠的 条评论
为什么被折叠?



