cocos2dx 数字以图片形式来展示的方法

感谢点评与关注,欢迎转载与分享。
勤奋努力,持之以恒!


下面封装方法性能不是最优仅供参考
更好方法请参考:http://blog.csdn.net/song_hui_xiang/article/details/42921537

//图片命名规则:number_0.png  fileName则为 number_  numSpace:图片间距 leftOrRight:1左对齐 2右对齐 3居中对齐
CCSprite* Utilities::numberChangeToImage(int number,float numSpace,const char* fileName,int leftOrRightOrCenter)
{
    CCSprite* base_sp;
    string num_str = Convert2String(number);
    int num_str_count = num_str.length();
    
    if (leftOrRightOrCenter == 1) {
        for (int i=0; i<num_str_count; i++) {
            string num_single = num_str.substr(i,1);
            string each_num_str = fileName+num_single+".png";
            CCSprite* sp_number = CCSprite::createWithSpriteFrameName(each_num_str.c_str());
            sp_number->setAnchorPoint(ccp(0, 0.5));
            if (i==0) {
                base_sp = sp_number;
            }else{
                sp_number->setPosition(ccp((base_sp->getContentSize().width+numSpace)*i, base_sp->getContentSize().height*0.5));
                base_sp->addChild(sp_number);
            }
        }
    }else if(leftOrRightOrCenter == 2){
        for (int i=num_str_count-1; i>-1; i--) {
            string num_single = num_str.substr(i,1);
            string each_num_str = fileName+num_single+".png";
            CCSprite* sp_number = CCSprite::createWithSpriteFrameName(each_num_str.c_str());
            sp_number->setAnchorPoint(ccp(1, 0.5));
            if (i==num_str_count-1) {
                base_sp = sp_number;
            }else{
                sp_number->setPosition(ccp(-(base_sp->getContentSize().width+numSpace)*(num_str_count-2-i), base_sp->getContentSize().height*0.5));
                base_sp->addChild(sp_number);
            }
        }
    }else if(leftOrRightOrCenter == 3){
        int centerIndex;
        if (num_str_count%2 == 0) {
            centerIndex = num_str_count/2;
        }else{
            centerIndex = (num_str_count+1)/2;
        }
        string centerFileName = fileName+num_str.substr(centerIndex-1,1)+".png";
        base_sp = CCSprite::createWithSpriteFrameName(centerFileName.c_str());
        if (num_str_count%2 == 0) {
            base_sp->setAnchorPoint(ccp(1, 0.5));
        }
        
        int endIndex = 1;
        for (int i=centerIndex; i<num_str_count; i++) {
            string num_single = num_str.substr(i,1);
            string each_num_str = fileName+num_single+".png";
            CCSprite* sp_number = CCSprite::createWithSpriteFrameName(each_num_str.c_str());
            sp_number->setAnchorPoint(ccp(0, 0.5));
            sp_number->setPosition(ccp((base_sp->getContentSize().width+numSpace)*endIndex, base_sp->getContentSize().height*0.5));
            base_sp->addChild(sp_number);
            endIndex++;
        }
        
        int startIndex = 1;
        for (int i=centerIndex-2; i>-1; i--) {
            string num_single = num_str.substr(i,1);
            string each_num_str = fileName+num_single+".png";
            CCSprite* sp_number = CCSprite::createWithSpriteFrameName(each_num_str.c_str());
            sp_number->setAnchorPoint(ccp(0, 0.5));
            sp_number->setPosition(ccp(-(base_sp->getContentSize().width+numSpace)*startIndex, base_sp->getContentSize().height*0.5));
            base_sp->addChild(sp_number);
            startIndex++;
        }
    }
    return base_sp;
}



你可能感兴趣的:(cocos2dx 数字以图片形式来展示的方法)