cocos2d-xccscrolllayer

CCScrollView with CCControlButton. How to control the touch area?


I have CCScrollView with container with CCControlButtons, when the buttons scroll out of the visible CCScrollView area, They are also can be touched too. How can I control the area ?


Solution

  • My problems: There is a scrollView with many buttons (items). Above it there are 2 function buttons (return, start).

    1. When I scroll down item buttons overlie function buttons. When I swallow all touches above my scrollview I will lose my function buttons. So I have to find another solution.

    2. When I start draging scroll view a item button is pressed. When I ended the button action will be execute. This is very annoying.

    But there is the solution. I have created new CCControlButton. It checks whether was clicked outside scrollview or was dragged. The button is used for items buttons.

    bool ControlButtonForScrolling::checkIfTouchIsInsideScrollView(CCTouch *pTouch)
    {
        CCPoint touchLocation = pTouch->getLocation(); // Get the touch position
        touchLocation = _scrollView->getParent()->convertToNodeSpace(touchLocation);
        CCRect bBox=_scrollView->boundingBox();
        bool result = bBox.containsPoint(touchLocation);
        return result;
    }
    
    bool ControlButtonForScrolling::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
    {
        bool isInside = this->checkIfTouchIsInsideScrollView(pTouch);
        if (isInside) {
            return CCControlButton::ccTouchBegan(pTouch, pEvent);
        }
        else
        {
            return false;
        }
    }
    
    void ControlButtonForScrolling::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
    {
        CCControlButton::ccTouchMoved(pTouch, pEvent);
        _scrollWasDragged = true; // information about dragging is stored to prevent sending action
    }
    
    void ControlButtonForScrolling::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
    {
        // this method is a copy of CCControlButton::ccTouchEnded except lines with _scrollWasDragged
        m_eState = CCControlStateNormal;
        m_isPushed = false;
        setHighlighted(false);
    
        if (!_scrollWasDragged)
        {
            if (isTouchInside(pTouch))
            {
                sendActionsForControlEvents(CCControlEventTouchUpInside);
            }
            else
            {
                sendActionsForControlEvents(CCControlEventTouchUpOutside);
            }
        }
        _scrollWasDragged = false;
    }