cocos2d-x-3.x bringToFront & sendToBack实现

前端之家收集整理的这篇文章主要介绍了cocos2d-x-3.x bringToFront & sendToBack实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
void Node::bringToFront(void)
{
    auto parent = this->getParent();
    if (parent != nullptr && parent->getChildrenCount() >= 2) {

        auto& siblings = parent->getChildren();

        auto last = *siblings.rbegin();
        auto topOrderOfArrival = last->getOrderOfArrival();
        auto topLocalZOrder = last->getLocalZOrder();

        for (size_t idx = siblings.size() - 1; idx > 0; --idx)
        {
            auto sibling = siblings.at(idx);
            if (sibling != this) {
                sibling->setOrderOfArrival(siblings.at(idx - 1)->getOrderOfArrival());
                sibling->_setLocalZOrder(siblings.at(idx - 1)->getLocalZOrder());
            }
            else {
                break;
            }
        }

        this->setOrderOfArrival(topOrderOfArrival);
        this->_setLocalZOrder(topLocalZOrder);

        std::sort(std::begin(siblings),std::end(siblings),nodeComparisonLess);
        _eventDispatcher->setDirtyForNode(this);
    }
}

void Node::sendToBack(void)
{
    auto parent = this->getParent();
    if (parent != nullptr && parent->getChildrenCount() >= 2) {

        auto& siblings = parent->getChildren();
        auto start = *siblings.begin();
        auto bottomOrderOfArrival = start->getOrderOfArrival();
        auto bottomLocalZOrder = start->getLocalZOrder();
        
        for (size_t idx = 0; idx < siblings.size() - 1; ++idx)
        {
            auto c = siblings.at(idx);
            if (c != this) {
                c->setOrderOfArrival(siblings.at(idx + 1)->getOrderOfArrival());
                c->_setLocalZOrder(siblings.at(idx + 1)->getLocalZOrder());
            }
            else {
                break;
            }
        }

        this->setOrderOfArrival(bottomOrderOfArrival);
        this->_setLocalZOrder(bottomLocalZOrder);

        std::sort(std::begin(siblings),nodeComparisonLess);
        _eventDispatcher->setDirtyForNode(this);
    }
}
原文链接:https://www.f2er.com/cocos2dx/346388.html

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