如何播放动画 (使用.plist)

来源:互联网 发布:java中的形参和实参 编辑:程序博客网 时间:2024/05/19 23:13
// on "init" you need to initialize your instancebool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !CCLayer::init() )    {        return false;    }     //将fish.pngs 缓存起来     CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("fish.plist");           sp4 = CCSprite::create();    sp4->setPosition(ccp(100, 100));        sp4->setRotation(90);        CCAnimate *spAni = playMC("fish07_0", 10, 0.2);    CCRepeatForever *rp = CCRepeatForever::create(spAni);       sp4->runAction(rp);    this->addChild(sp4, 6);            return true;}cocos2d::CCAnimate* HelloWorld::playMC(const char *spName, int count, float d){       CCArray* arr = CCArray::createWithCapacity(count + 1);    CCSpriteFrameCache* spCache = CCSpriteFrameCache::sharedSpriteFrameCache();    char url[100] = {0};    for (int i = 1; i < count + 1; i++) {        sprintf(url, "%s%i.png",spName, i);//fish01_01.png        CCLog("%s", url);      CCSpriteFrame *f = spCache->spriteFrameByName(url);        if(f != NULL)arr->addObject(f);        else break;    }        CCAnimation* an = CCAnimation::createWithSpriteFrames(arr, d);        return CCAnimate::create(an);}


注意点:

1. char 传参数类型 const char *

2. sprintf使用:  sprintf(url, "%s%i.png",spName, i);//fish01_01.png