cocos2d-x点击事件

前端之家收集整理的这篇文章主要介绍了cocos2d-x点击事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

xxx.h文件

Button* bt1;
    Button* bt2;

    void MyTouch1(cocos2d::Ref *pSender,Widget::TouchEventType type);

xxx.cpp文件

auto rootNode = CSLoader::createNode("SelectServerScene.csb");
    cocos2d::ui::Layout* Panel_1 = (cocos2d::ui::Layout*)rootNode->getChildByName("Panel_1");

    bt1 = (Button*)Helper::seekWidgetByName(Panel_1,"Button_1");
    bt2 = (Button*)Helper::seekWidgetByName(Panel_1,"Button_2");

    bt1->addTouchEventListener(CC_CALLBACK_2(SelectServer::MyTouch1,this));
    bt2->addTouchEventListener(CC_CALLBACK_2(SelectServer::MyTouch1,this));

    bt1->setTag(1);
    bt2->setTag(2);

    addChild(rootNode);

//pSender谁申请事件就是谁
void SelectServer::MyTouch1(cocos2d::Ref * pSender,Widget::TouchEventType type)
{
    switch (type)
    {
    case cocos2d::ui::Widget::TouchEventType::BEGAN:
        //手指点中按钮,没有弹起
        CCLOG("BEGAN");
        break;
    case cocos2d::ui::Widget::TouchEventType::MOVED:
        //手指点中按钮,在上面滑动。
        CCLOG("MOVED");
        break;
    case cocos2d::ui::Widget::TouchEventType::ENDED:
        //手指点中按钮,松开。
        CCLOG("ENDED");
        if (((ImageView*)pSender)->getTag() == 1) {
            CCLOG("backButton");

        }

        if (((ImageView*)pSender)->getTag() == 2) {
            CCLOG("okButton");

        }

        break;
    case cocos2d::ui::Widget::TouchEventType::CANCELED:
        //手指点中按钮,手指滑动到按钮之外,然后再松开。
        CCLOG("CANCELED");
        break;
    default:
        break;
    }
}
原文链接:https://www.f2er.com/cocos2dx/340512.html

猜你在找的Cocos2d-x相关文章