CCSprite
通过.plist创建, warrior.plist和warrior.pvr.ccz 可以通过TexturePacker 工具生成,个人比较推荐这种方法。
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("warrior.plist"); CCSpriteBatchNode* spriteBatch = CCSpriteBatchNode::create("warrior.pvr.ccz"); this->addChild(spriteBatch); CCArray* frames = CCArray::createWithCapacity(4); CCSprite* sprite = CCSprite::createWithSpriteFrameName("test_01.png"); sprite->setPosition(ccp(SCEEN_SIZE.width/2, SCEEN_SIZE.height/2)); for(int i=0; i<4; i++) { CCSpriteFrame* frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::createWithFormat("test_0%d.png", i+1)->getCString()); frames->addObject(frame); } CCAnimation* ani = CCAnimation::createWithSpriteFrames(frames, 1.0f/12.0f); CCAction* action = CCRepeatForever::create(CCAnimate::create(ani)); sprite->runAction(action); spriteBatch->addChild(sprite);
通过CCTexture2d创建
CCTexture2D* tex = CCTextureCache::sharedTextureCache()->addImage(filename1); tex->setAliasTexParameters();
//frameCount:表示sprite有几帧 CCArray* frames = CCArray::createWithCapacity(frameCount); for(int i=0; i<frameCount; i++) { CCSpriteFrame* frame = CCSpriteFrame::createWithTexture(tex, CCRectMake(0, tex->getPixelsHigh() / frameCount * i, tex->getPixelsWide(), tex->getPixelsHigh()/frameCount)); frames->addObject(frame); } CCAnimation* swimAni = CCAnimation::createWithSpriteFrames(frames, 1.0f / 30); this->initWithSpriteFrame((CCSpriteFrame*)(frames->objectAtIndex(0))); CCAction* action = CCRepeatForever::create(CCAnimate::create(swimAni)); this->runAction(action);