cocos2dx 触摸事件

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

单点触摸:

auto label = LabelTTF::create("Label","Courier",30);

    label->setPosition(visibleSize / 2);

    this->addChild(label);

    auto listener = EventListenerTouchOneByOne::create(); /*创建事件侦听*/

    listener->onTouchBegan = [](Touch *touch,Event* event){ /*开始点击触发回调函数*/

        if (event->getCurrentTarget()->getBoundingBox().containsPoint( /*触摸点是否在目标范围*/ touch->getLocation())){ CCLOG("OK"); } return false; /*事件是否传递 false不传递,true传递*/ }; /*在场景中注册事件*/ Director::getInstance()->getEventDispatcher()-> 
        addEventListenerWithSceneGraPHPriority(listener,label);

多点触摸:

auto listener = EventListenerTouchAllAtOnce::create();

    listener->onTouchesBegan = [](std::vector<Touch*> touchs,Event* event){
        CCLOG("Touchs Began!");
    };

    listener->onTouchesMoved = [](std::vector<Touch*> touchs,Event* event){
        CCLOG("Touchs Move: %d",touchs.size());
    };


    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,this);
原文链接:https://www.f2er.com/cocos2dx/340100.html

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