給Layer添加觸摸事件

給Layer添加觸摸事件能夠在init方法中添加如下代碼:this

 

    auto touchlistener = EventListenerTouchOneByOne::create();
    touchlistener->setSwallowTouches(true);//設置是否想下傳遞觸摸
    touchlistener->onTouchBegan = CC_CALLBACK_2(DirectionController::onTouchBegan, this);
    touchlistener->onTouchMoved = CC_CALLBACK_2(DirectionController::onTouchMoved, this);
    touchlistener->onTouchEnded = CC_CALLBACK_2(DirectionController::onTouchEnded, this);
    
    Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(touchlistener, 1.0);

  

而後就能夠實現一下各個方法了spa

bool DirectionController::onTouchBegan(Touch* pTouch, Event* pEvent) {
    
    return true;
}

void DirectionController::onTouchMoved(Touch* pTouch, Event* pEvent) {
    Point touchPoint = pTouch->getLocationInView();
    Point touchGLPoint = Director::getInstance()->convertToGL(touchPoint);
}

void DirectionController::onTouchEnded(Touch* pTouch, Event* pEvent) {

}
相關文章
相關標籤/搜索