cocos2dx 按钮长按事件

 通过touchBegin 结合 schedule 来实现

void UILayer::longTouchUpdate(float delay){
m_isLongPress = true;
if (m_longTouchButton != NULL){
if (m_longTouchButton->getName() == “jump_Btn”){
m_hero->floating();
}
}
}

// 长按事件的update 计时器

if (type == Widget::TouchEventType::BEGAN && m_hero != NULL)
{
schedule(schedule_selector(UILayer::longTouchUpdate), 0.5f);
else if (node->getName() == “jump_Btn”)
{
m_hero->jump();
m_longTouchButton = node;
}
}
在事件begin中开启 schedule , 并且获得长按按钮的引用。

if (type == Widget::TouchEventType::ENDED && m_hero != NULL){
unschedule(schedule_selector(UILayer::longTouchUpdate));
if (m_isLongPress == true)
{
if (m_longTouchButton->getName() == “jump_Btn”)
{
m_hero->finishFloating();
}
m_longTouchButton = NULL;
m_isLongPress = false;
}

// 按钮抬起时将长按按钮清空

你可能感兴趣的:(cocos-x笔记)