cocos2d-x使用图层进行触摸处理

时间:2014-11-14 18:51:00

标签: c++ touch cocos2d-x layer cocos2d-x-3.0

我遇到cocos2d-x touch处理器的问题。我有两节课。每个类都有一个场景和一个图层。我在两个图层上都有精灵,它们处理相同的功能。问题是,当我从A层转换到B层并点击精灵时,触摸处理程序从A层获取精灵,但我需要来自B层的精灵,显然我点击了B层。据我了解,问题出在setPriority?你能帮我解决一下这个问题吗?感谢

CardItem是我的精灵

void CardItem::addEvents()
{
    auto listener = cocos2d::EventListenerTouchOneByOne::create();
    listener->setSwallowTouches(true);

    listener->onTouchBegan = [&](cocos2d::Touch* touch, cocos2d::Event* event)
    {           
        cocos2d::Vec2 p = touch->getLocation();
        cocos2d::Rect rect = this->getBoundingBox();        

        if(rect.containsPoint(p))
        {
            return true; // to indicate that we have consumed it.
        }


        return false; // we did not consume this event, pass thru.

    };
    listener->onTouchEnded = [=](cocos2d::Touch* touch, cocos2d::Event* event)
    {
        //I have here some code
    };
    cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 30);
}

void CardItem::touchEvent(cocos2d::Touch* touch, cocos2d::Vec2 _point)
{
    auto *pNode = this->getParent(); // here is "pNode" always gets layer A 
}

这就是我如何改变A层和B层之间的场景

void GameBoardLayer::showGameBoardComputer()
{
    pGameBoardComputerScene = GameBoardComputerScene::create();
    auto pGameBoardComputerLayer = pGameBoardComputerScene->getLayer(); 
    Director::getInstance()->pushScene(pGameBoardComputerScene);    
}

void GameBoardComputerLayer::gameBoardComputerItemBackCallback(Ref* pSender)
{
    Director::getInstance()->popScene();
}

因此,当我点击精灵时,无论它是什么层,它总是点击A层到B层

0 个答案:

没有答案