cocos2dx3.0--touchevent

 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::onTouchBeganthis);

    listener->onTouchMoved = CC_CALLBACK_2(GameMain::onTouchMovedthis);

    listener->onTouchEnded = CC_CALLBACK_2(GameMain::onTouchEndedthis);

    listener->onTouchCancelled = CC_CALLBACK_2(GameMain::onTouchCancelledthis);

    

    _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); 



 


转载于:https://my.oschina.net/u/188924/blog/280890

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值