From b4c8d88707d6b7e2883e099289570fe467511aca Mon Sep 17 00:00:00 2001 From: Ming Date: Fri, 13 Aug 2010 02:54:30 +0000 Subject: [PATCH] issue #45: fix logic error of insertChild() --- cocos2dx/sprite_nodes/CCSpriteSheet.cpp | 47 ++++++++++++++++++------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/cocos2dx/sprite_nodes/CCSpriteSheet.cpp b/cocos2dx/sprite_nodes/CCSpriteSheet.cpp index c3f5ca1509..3cf167cfc7 100644 --- a/cocos2dx/sprite_nodes/CCSpriteSheet.cpp +++ b/cocos2dx/sprite_nodes/CCSpriteSheet.cpp @@ -449,25 +449,46 @@ void CCSpriteSheet::insertChild(CCSprite *pobSprite, unsigned int uIndex) m_pobDescendants->insertObjectAtIndex(pobSprite, uIndex); // update indices - unsigned int u = uIndex + 1; - - NSMutableArray::NSMutableArrayIterator iter; - CCSprite *pSprite; - for (iter = m_pobDescendants->begin(); iter != m_pobDescendants->end(); ++iter) + unsigned int i = 0; + if (m_pobDescendants && m_pobDescendants->count() > 0) { - CCSprite *child = *iter; - child->setAtlasIndex(child->getAtlasIndex() + 1); - } + NSMutableArray::NSMutableArrayIterator iter; + for (iter = m_pobDescendants->begin(); iter != m_pobDescendants->end(); ++iter) + { + if (! *iter) + { + break; + } + + if (i > uIndex) + { + (*iter)->setAtlasIndex((*iter)->getAtlasIndex() + 1); + } + + ++i; + } + } // add children recursively NSMutableArray *pChildren = pobSprite->getChildren(); - NSMutableArray::NSMutableArrayIterator iterNode; - for (iterNode = pChildren->begin(); iterNode != pChildren->end(); ++iterNode) + if (pChildren && pChildren->count() > 0) { - pSprite = static_cast(*iterNode); - unsigned int uIndex = atlasIndexForChild(pSprite, pSprite->getZOrder()); - insertChild(pSprite, uIndex); + NSMutableArray::NSMutableArrayIterator iterNode; + CCSprite *pSprite; + for (iterNode = pChildren->begin(); iterNode != pChildren->end(); ++iterNode) + { + pSprite = static_cast(*iterNode); + + if (! pSprite) + { + break; + } + + unsigned int uIndex = atlasIndexForChild(pSprite, pSprite->getZOrder()); + insertChild(pSprite, uIndex); + } } + } void CCSpriteSheet::removeSpriteFromAtlas(CCSprite *pobSprite)