给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) {

}
相关文章
相关标签/搜索