Cocos2d-x How to make animation with CCRepeatForever

CCRepeatForever* Global::genAnimation (const char* shadowName ,float delay ,int nRow ,int nCol ,int lastNum) {
    int rFrame,cFrame;
    if(lastNum!=0) {
        rFrame = nRow - 1;
        cFrame = lastNum - 1;
    } else {
        rFrame = nRow ;
        cFrame = nCol;
    }
   
    /* setting animation for sprite */
    if(strlen(shadowName) > 0 ) {
        CCSize size = Global::getImageSize(shadowName);
        int width = size.width/nCol;
        int height = size.height/nRow;
       
        CCArray *animationFrames = CCArray::createWithCapacity(nRow*nCol);
       
        for (int i = 0; i < nRow; i++) {
            for (int j = 0; j < nCol; j++) {
                animationFrames->addObject(Global::getFrameFromImage(shadowName,j,i ,width ,height));
                if(lastNum > 0) {
                    if((i == nRow - 1) && (j == lastNum - 1)) break;
                }
            }
        }
       
        CCAnimation *animation = CCAnimation::createWithSpriteFrames(animationFrames ,delay);
        CCAnimate *animate = CCAnimate::create(animation);
        CCRepeatForever *repeat = CCRepeatForever:: create(animate);
        return repeat;
       
    }
   
    return NULL;

};


CCRepeatForever* Global::genAnimationFromImages (CCArray* imageArray,float delay) {
    /* setting animation for sprite */
   
    CCArray *animationFrames = CCArray::createWithCapacity(imageArray->count());
   
    for (int i = 0; i < imageArray->count(); i++) {
       
        CCString *file = (CCString*)imageArray->objectAtIndex(i);
        CCSize size = Global::getImageSize(file->getCString());
        int width = size.width;
        int height = size.height;
        animationFrames->addObject(Global::getFrameFromImage(file->getCString(),0 ,0 ,width ,height));
    }
    CCAnimation *animation = CCAnimation::createWithSpriteFrames(animationFrames ,delay);
    CCAnimate *animate = CCAnimate::create(animation);
    CCRepeatForever *repeat = CCRepeatForever:: create(animate);
    return repeat;   
}

Comments

Popular posts from this blog

Java : Variables Declaring

Install DNF in RHEL/CentOS 7

SQL Self JOIN