2014-01-23 180 views
0

據我所知它不存在。CCArray枚舉器對象

CCArray *array = CCArray::create(); 
    CCArrayEnumerator *enumerator = array->createEnumerator(); 
    ... 

    CCObject *nextObjectOrNull = enumerator->nextObject(); 
    CCObject *currentObjectOrNull = enumerator->peekCurrentObject(); 

這個類將使我的代碼更簡單,如果沒有人編碼它,我會立即做到這一點。但我還沒有找到任何有關這個班級需求的請求或論壇帖子。這很奇怪。

+0

...和你的問題是? – LearnCocos2D

回答

0

我寫了一個簡單的類,它不工作。歡迎提出建議。

CCArrayEnumerator.h:

#include "cocos2d.h" 

class CCArrayEnumerator : public cocos2d::CCObject{ 

public: 
    static CCArrayEnumerator* create(cocos2d::CCArray* array); 
    cocos2d::CCObject* nextObject(); 
    cocos2d::CCObject* peekCurrentObject(); 
private: 
    cocos2d::CCArray* _array; 
    CCArrayEnumerator(); 
    ~CCArrayEnumerator(); 
    virtual bool init(cocos2d::CCArray* array); 

    int _currentIndex; 
}; 

CCArrayEnumerator.cpp:

#include "CCArrayEnumerator.h" 
USING_NS_CC; 

CCArrayEnumerator* CCArrayEnumerator::create(cocos2d::CCArray* array){ 
    CCArrayEnumerator *pRet = new CCArrayEnumerator(); 
    if (pRet && pRet->init(array)) 
    { 
     pRet->autorelease(); 
     return pRet; 
    } 
    else 
    { 
     CC_SAFE_DELETE(pRet); 
     return NULL; 
    } 
} 

    cocos2d::CCObject* CCArrayEnumerator::nextObject(){ 
    CCObject *retval = NULL; 

    if(_array->count() > _currentIndex + 1){ 
     _currentIndex++; 
     retval = _array->objectAtIndex(_currentIndex); 
    } 

    return retval; 
} 

cocos2d::CCObject* CCArrayEnumerator::peekCurrentObject(){ 
    CCObject *retval = NULL; 

    if(_currentIndex != -1 && _array->count() > _currentIndex){ 
     retval = _array->objectAtIndex(_currentIndex); 
    } 

    return retval; 
} 

CCArrayEnumerator::CCArrayEnumerator(): 
_array(NULL) 
,_currentIndex(-1){ 

} 

CCArrayEnumerator::~CCArrayEnumerator(){ 
    if(_array){ 
     _array->release(); 
     _array = NULL; 
    } 
} 

bool CCArrayEnumerator::init(cocos2d::CCArray* array){ 

    // Superclass CCObject has no init. 

    CCAssert(_array == NULL,"CCArrayEnumerator is already initialized."); 

    _array = array; 
    _array->retain(); 

    return true; 
} 
1

你可以編寫你自己的代碼,但已經有一個可以用於相同的宏。

這裏是僞代碼:

CCArray *array = this->getChildren(); 

CCObject* currentObject = NULL; 
CCARRAY_FOREACH(array , currentObject) 
{ 
    CCNode *node = (CCNode*)currentObject; 
}