cocos2d-x触摸事件(一)

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

xxx.h文件

bool onMapTouchBegan(cocos2d::Touch *touch,cocos2d::Event *event);
    void onMapTouchMove(cocos2d::Touch *touch,cocos2d::Event *event);
    void onMapTouchEnded(cocos2d::Touch *touch,cocos2d::Event *event);

xxx.cpp文件

//1.单点触摸
    auto mapOneByOne = EventListenerTouchOneByOne::create();
    mapOneByOne->setSwallowTouches(true);//其它事件都吞没掉(点击一个对象,其它对象的事件是否被吞没)
    mapOneByOne->onTouchBegan = CC_CALLBACK_2(MainMap::onMapTouchBegan,this);
    mapOneByOne->onTouchMoved = CC_CALLBACK_2(MainMap::onMapTouchMove,this);
    mapOneByOne->onTouchEnded = CC_CALLBACK_2(MainMap::onMapTouchEnded,this);
    //添加触摸事件
    _eventDispatcher->addEventListenerWithSceneGraPHPriority(mapOneByOne,imgMap);

bool MainMap::onMapTouchBegan(cocos2d::Touch * touch,cocos2d::Event * event)
{
    CCLOG("onMapTouchBegan");
    return true;
}

void MainMap::onMapTouchMove(cocos2d::Touch * touch,cocos2d::Event * event)
{
        CCLOG("onMapTouchMove");

        //手指滑动的位置距离
        Vec2 diff = touch->getDelta();
        //地图原来的位置
        Vec2 posSrc = imgMap->getPosition();
        //滑动后的位置
        Vec2 posDes = posSrc + diff;

        //判断不能拖到屏幕以外
        if (posDes.x >= 1280) {
            posDes.x = 1280;
        }
        if (posDes.y >= 814) {
            posDes.y = 814;
        }
        if (posDes.x <= (960 - 1280)) {
            posDes.x = 960 - 1280;
        }
        if (posDes.y <= (640 - 814)) {
            posDes.y = 640 - 814;
        }
        //图片滑动后的位置
        //imgMap->setPosition(posDes);
}

void MainMap::onMapTouchEnded(cocos2d::Touch * touch,cocos2d::Event * event)
{
    CCLOG("onMapTouchEnded");
}
原文链接:https://www.f2er.com/cocos2dx/340515.html

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