码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-11-05 17:19:25      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:blog   ar   os   for   sp   on   2014   art   log   

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), std::end(siblings), nodeComparisonLess);
        _eventDispatcher->setDirtyForNode(this);
    }
}

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

标签:blog   ar   os   for   sp   on   2014   art   log   

原文地址:http://blog.csdn.net/xseekerj/article/details/40824573

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!