求助:关于setSwallowTouches()的用法

之前在网上搜到有人对于这个方法的解释是这样的:
// true不向下触摸,简单点来说,比如有两个sprite ,A和 B,A在上B在下(位置重叠),触摸A的时候,B不会受到影响,反之false,向下传递触摸,触摸A也等于触摸了B

于是我写了一下代码,建了三个精灵,sprite1在sprite2上面,测试点击sprite1,会不会出发sprite2的事件,sprite3用于测试前两个精灵的点击事件。

问题:无论设置时true还是false,结果sprite1 和sprite2 的事件都触发了,咋回事呢,求大神:5:

bool TouchLayer::init()
{
    if (!Layer::init())
    {
        return false;
    }
    auto visibleSize = Director::getInstance()->getVisibleSize();

auto listener = EventListenerTouchOneByOne::create();
    listener->setSwallowTouches(false);

    auto sprite2 = Sprite::create("turtle.png");
    sprite2->setPosition(Vec2(visibleSize.width/4,visibleSize.height/4));
    this->addChild(sprite2,0);

    auto sprite1 = Sprite::create("snail.png");
    sprite1->setPosition(Vec2(visibleSize.width/4,visibleSize.height/4));
    this->addChild(sprite1,1);

    auto sprite3 = Sprite::create("lobster.png");
    sprite3->setPosition(Vec2(visibleSize.width/4,visibleSize.height*3/4));
    this->addChild(sprite3);

    listener->onTouchBegan = =](Touch* _touch,Event* _event){

        auto touchSpritePoint = convertTouchToNodeSpace(_touch);

        if (sprite1->boundingBox().containsPoint(touchSpritePoint))
        {
        

            auto actionMoveBySprite = MoveBy::create(1.0f,Vec2(100,0));
            sprite3->runAction(actionMoveBySprite);
        }

        if (sprite2->boundingBox().containsPoint(touchSpritePoint))
        {
            

            auto actionRotateBySprite = RotateBy::create(1.0f,180);
            sprite3->runAction(actionRotateBySprite);
        }
        return true;
    };
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,sprite1);
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener->clone(),sprite2);

    return true;
}

```
小弟最近在学习cocos2d 3.0中的触摸事件,遇到一个关于setSwallowTouches(true) 用法的问题

已解决,低级错误

    auto listener = EventListenerTouchOneByOne::create();
    listener->setSwallowTouches(true);
    
    auto listener2 = EventListenerTouchOneByOne::create();

    auto sprite2 = Sprite::create("turtle.png");
    sprite2->setPosition(Vec2(visibleSize.width/4,visibleSize.height/4));
    this->addChild(sprite2,0);

    auto sprite1 = Sprite::create("snail.png");
    sprite1->setPosition(Vec2(visibleSize.width/4,visibleSize.height/4));
    this->addChild(sprite1,1);

    auto sprite3 = Sprite::create("lobster.png");
    sprite3->setPosition(Vec2(visibleSize.width/4,visibleSize.height*3/4));
    this->addChild(sprite3);

    listener->onTouchBegan = =](Touch* _touch,Event* _event){

        auto touchSpritePoint = convertTouchToNodeSpace(_touch);

        if (sprite1->boundingBox().containsPoint(touchSpritePoint))
        {
        

            auto actionMoveBySprite = MoveBy::create(1.0f,Vec2(100,0));
            sprite3->runAction(actionMoveBySprite);
        }

    
        return true;
    };

    listener2->onTouchBegan = =](Touch* _touch,Event* _event){

        auto touchSpritePoint = convertTouchToNodeSpace(_touch);

        if (sprite2->boundingBox().containsPoint(touchSpritePoint))
        {


            auto actionRotateBySprite = RotateBy::create(1.0f,180);
            sprite3->runAction(actionRotateBySprite);
        }
        return true;
    };

    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,sprite1);
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener2,sprite2);

    return true;
}


```

auto touchSpritePoint = convertTouchToNodeSpace(_touch);
为什么我学着你敲,学习
但我的(_touch)没有合适的构造函数中存在的转换什么的
错误的

源码贴出来看看

bool MainGame::init()
{
if(!Layer::init())
return false;

m_size = Director::getInstance()->getWinSize();


auto background = Background::create();
this->addChild(background);

m_listener = EventListenerKeyboard::create();
m_listener->onKeyReleased = CC_CALLBACK_2(MainGame::onKeyReleased,this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(m_listener,this);

SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Player.plist");
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("EnemyCar.plist");

m_player = Player::create();
m_player->setPosition(Vec2(m_size.width/2, m_size.height*0.15));
this->addChild(m_player);

auto turnRight = Sprite::create("turnRight.png");
turnRight->setTag(200);
turnRight->setPosition(Director::getInstance()->convertToGL(Vec2(600, 800)));
this->addChild(turnRight);

auto turnLeft = Sprite::create("turnLeft.png");
turnLeft->setTag(300);
turnLeft->setPosition(Director::getInstance()->convertToGL(Vec2(40, 800)));
this->addChild(turnLeft);

auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);

auto listener2 = EventListenerTouchOneByOne::create();

listener->onTouchBegan = =](Touch * _touch, Event * _event)
{
    auto touchSpritePoint = convertToNodeSpace(_touch);
    if(turnRight->boundingBox().containsPoint(touchSpritePoint))
    {
        auto actionMoveBySprite = MoveBy::create(1.0f, Vec2(600, 800));
        m_player->runAction(actionMoveBySprite);
    }

    return true;
};

或是需要在头文件里加些什么吗?

    listener->onTouchBegan = =](Touch * _touch, Event * _event)
    {
        auto touchSpritePoint = convertToNodeSpace(_touch);
        if(turnRight->boundingBox().containsPoint(touchSpritePoint))
        {
            auto actionMoveBySprite = MoveBy::create(1.0f, Vec2(600, 800));
            m_player->runAction(actionMoveBySprite);
        }

        return true;
    };


```


你把这段代码改一下,试试,我看你之前把turnRight的坐标转换成GL坐标了,然后又把_touch转换成节点坐标了,应该是这里出问题了
改成这样看看
    listener->onTouchBegan = =](Touch * _touch, Event * _event)
    {
        //auto touchSpritePoint = convertToNodeSpace(_touch);
        // getLocation返回GL坐标
        if(turnRight->boundingBox().containsPoint(_touch->getLocation()))
        {
            auto actionMoveBySprite = MoveBy::create(1.0f, Vec2(600, 800));
            m_player->runAction(actionMoveBySprite);
        }

        return true;
    };


```

原来是这个问题,OK了,thank
还想问一个,比如你这样设置的这个touch机制,要不要remove,删除的?

用 addEventListenerWithSceneGraphPriority() 就不用手动删除
用 addEventListenerWithFixedPriority() 就需要

原来这样, 非常感谢。
还有一个
listener->onTouchBegan = =](Touch * _touch, Event * _event)

那个 =] 是什么写法,不懂

— Begin quote from ____

引用第8楼lukemyh于2014-08-06 16:37发表的 回 7楼(suicici) 的帖子 :
原来这样, 非常感谢。
还有一个
listener->onTouchBegan = =](Touch * _touch, Event * _event)

那个 =] 是什么写法,不懂 http://www.cocoachina.com/bbs/job.php?action=topost&tid=218760&pid=1023050

— End quote

lamda表达式,c++11的特性,2dx 3.0以上支持

] 表示不引用前文出现的任何对象
= ] 表示可以引用前文出现过的任何对象
表示引用特定的对象
&] 表示引用前文的对象,并可以对其进行修改

那么用你的那个方法,有什么办法可以不让精灵移除屏幕边框,也就是当你移动到窗口的边框的时候,再怎么按都只会停在那里

哪的问题 ? 也不标记一下 :)

好吧…我承认我也没看出来你哪里出了问题.~!! 不能标记下咩…:12:

表示允许以传值方式,访问外围环境变量,如果改成 &] 则表示以传址方式访问,也可以混合模式,例如 &, paramName/例外,传值/]