usage:
use like below in any class extends node. the class must has onTouchBegan & onTouchMoved & onTouchEnded & onTouchCancelled.
when onTouchBegan return true, onTouchEnded could receive the event.
GameMain::init()
{
if (!CCLayerColor::init())
{
return false;
}
auto listener = EventListenerTouchOneByOne::create();//创建一个触摸监听
listener->setSwallowTouches(true);//设置是否想下传递触摸
listener->onTouchBegan = CC_CALLBACK_2(GameMain::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(GameMain::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(GameMain::onTouchEnded, this);
listener->onTouchCancelled = CC_CALLBACK_2(GameMain::onTouchCancelled, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
bool GameMain::onTouchBegan(Touch *touch, Event *unused_event){
log("%s","GameMain onTouchBegan");
return true;
}
void GameMain::onTouchMoved(Touch *touch, Event *unused_event){
log("%s","GameMain onTouchMoved");
}
void GameMain::onTouchEnded(Touch *touch, Event *unused_event){
log("%s","GameMain onTouchEnded");
}
void GameMain::onTouchCancelled(Touch *touch, Event *unused_event){
log("%s","GameMain onTouchCancelled");
}
==============================================================================================
when onTouchBegan return true, onTouchEnded could receive the event.
void EventDispatcher::dispatchTouchEvent(EventTouch* event)
if (eventCode == EventTouch::EventCode::BEGAN)
{
if (listener->onTouchBegan)
{
isClaimed = listener->onTouchBegan(*touchesIter, event);
if (isClaimed && listener->_isRegistered)
{
listener->_claimedTouches.push_back(*touchesIter);
}
}
}
==============================================================================================
1.从不同平台的glview传递而下
void GLView::onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify)
{
if(GLFW_MOUSE_BUTTON_LEFT == button)
{
if(GLFW_PRESS == action)
{
_captured = true;
if (this->getViewPortRect().equals(Rect::ZERO) || this->getViewPortRect().containsPoint(Point(_mouseX,_mouseY)))
{
int id = 0;
this->handleTouchesBegin(1, &id, &_mouseX, &_mouseY);
}
}
else if(GLFW_RELEASE == action)
{
_captured = false;
if (this->getViewPortRect().equals(Rect::ZERO) || this->getViewPortRect().containsPoint(Point(_mouseX,_mouseY)))
{
int id = 0;
this->handleTouchesEnd(1, &id, &_mouseX, &_mouseY);
}
}
}
}
2.继而进行按键分发
void GLViewProtocol::handleTouchesBegin(int num, int ids[], float xs[], float ys[])
{
touchEvent._eventCode = EventTouch::EventCode::BEGAN;
auto dispatcher = Director::getInstance()->getEventDispatcher();
dispatcher->dispatchEvent(&touchEvent);
}
==============================================================================================
1.mainloop
void DisplayLinkDirector::mainLoop()
{
if (_purgeDirectorInNextLoop)
{
_purgeDirectorInNextLoop = false;
purgeDirector();
}
else if (! _invalid)
{
drawScene();
// release the objects
PoolManager::getInstance()->getCurrentPool()->clear();
}
}
2.drawscene
void Director::drawScene()
3.dispatchevent
_eventDispatcher->dispatchEvent(_eventAfterUpdate);