cocos2d-x touch handling with layers

1.1k Views Asked by At

I have a problem with cocos2d-x touch handler. I have two classes. Each class has one scene and one layer.I have sprites on both layers, and they handle in the same function. The problem is when I make the transition from layer A to layer B and click on the sprite, so touch handler gets sprite from layer A, but I need sprite from layer B, obviously that I click on layer B. As far as I understood, the problem is in setPriority? Could you please help me solve this problem? Thanks

CardItem is my sprite

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 
}

that's how I change scenes beetween layer A and B

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

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

so when I click on sprite no matter what layer it is, it always clicks on layer A through layer B

0

There are 0 best solutions below