auto listenter = EventListenerTouchOneByOne::create();//设置单点触摸 listenter->onTouchBegan = [](Touch* pTouch,Event* event)->bool { Point pos1 = pTouch->getLocation();// 3D 坐标 Point Pos2 = pTouch->getLocationInView();//2D 坐标 Point pos3 = Director::getInstance()->convertToGL(Pos2);//cocos 坐标 CCLOG("TouchBegin pos1.x = %lf,pos1.y = %lf",pos1.x,pos1.y); CCLOG("TouchBegin pos2.x = %lf,pos2.y = %lf",Pos2.x,Pos2.y); CCLOG("TouchBegin pos3.x = %lf,pos3.y = %lf",pos3.x,pos3.y); return true; }; listenter->onTouchMoved = [](Touch*,Event* e) { CCLOG("OnTuchMoved"); }; listenter->onTouchEnded = [](Touch*,Event* e) { CCLOG("OnTouchEnded"); }; _eventDispatcher->addEventListenerWithSceneGraPHPriority(listenter,this);
2
Size VisibleSize = Director::getInstance()->getVisibleSize(); Sprite* p1 = Sprite::create("Images/grossinis_sister1.png"); p1->setPosition(Point(VisibleSize.width * 0.5f-5,VisibleSize.height * 0.5f)); this->addChild(p1,1); Sprite* p2 = Sprite::create("Images/grossinis_sister2.png"); p2->setPosition(Point(VisibleSize.width * 0.5f+5,VisibleSize.height * 0.5f)); this->addChild(p2,0); auto Listenter = EventListenerTouchOneByOne::create(); Listenter->setSwallowTouches(true);//设置吞没事件 Listenter->onTouchBegan = [](Touch* pTouch,Event* pEvent) { auto target = static_cast<Sprite*>(pEvent->getCurrentTarget()); Point Pos = Director::getInstance()->convertToGL(pTouch->getLocationInView()); if(target->getBoundingBox().containsPoint(Pos))//判断是否在精灵中 { target->setOpacity(100);//设置透明度100 return true; } return false; }; Listenter->onTouchEnded = [](Touch* pTouch,Event* pEvent) { auto Target =(Sprite*) pEvent->getCurrentTarget(); Target->setOpacity(255); }; _eventDispatcher->addEventListenerWithSceneGraPHPriority(Listenter,p1); _eventDispatcher->addEventListenerWithSceneGraPHPriority(Listenter->clone(),p2);原文链接:https://www.f2er.com/cocos2dx/342747.html