cocos2dx单点触控

来源:互联网 发布:免费ppt模板下载 知乎 编辑:程序博客网 时间:2024/05/28 08:29

部分程序代码:

auto listener = EventListenerTouchOneByOne::create();    listener->onTouchBegan = CC_CALLBACK_2(CurveLayer::touchBegan, this);    listener->onTouchMoved = CC_CALLBACK_2(CurveLayer::touchMoved, this);    listener->onTouchEnded = CC_CALLBACK_2(CurveLayer::touchEnded, this);    listener->onTouchCancelled = CC_CALLBACK_2(CurveLayer::touchCancelled,this);    //两种都可以    //    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);    Director::getInstance()->getEventDispatcher()->    addEventListenerWithSceneGraphPriority(listener,this);

回掉函数:

bool CurveLayer::touchBegan(cocos2d::Touch *touch, cocos2d::Event *event){    Vec2 pos=touch->getLocation();    log("touchBegan x:%f,y:%f",pos.x,pos.y);    return true;//返回false时,触摸移动、触摸结束不会触发}void CurveLayer::touchMoved(Touch*touch, Event*event){    Vec2 pos=touch->getLocation();    log("touchMoved x:%f,y:%f",pos.x,pos.y);    }}void CurveLayer::touchEnded(Touch*touch, Event*event){    Vec2 pos=touch->getLocation();    log("touchEnded x:%f,y:%f",pos.x,pos.y);}void CurveLayer::touchCancelled(cocos2d::Touch*touch, cocos2d::Event*event){    Vec2 pos=touch->getLocation();    log("touchCancelled x:%f,y:%f",pos.x,pos.y);}

CurveLayer是自己定义的层。

addEventListenerWithSceneGraphPriority该单点触摸时间优先级与所在的节点优先级相同。

相应的还有设置固定优先级等函数详情见:
http://api.cocos.com/d2/d1b/classcocos2d_1_1_event_dispatcher.html#a9137cbc969607bc1c0f539155af16f15

设置允许向下传递 listener->setSwallowTouches(false)