cocos2d-x之TexturePacker使用
TexturePacker用来把大量图片批量导入,简化开发,直接贴代码
用来生成plist的原始图为A1_0.png到A1_3.png
1 CCSize size = CCDirector::sharedDirector()->getWinSize(); //获得屏幕的尺寸 2 3 CCArray* pArray = CCArray::createWithCapacity(4); 4 CCSpriteFrameCache* pCache = CCSpriteFrameCache::sharedSpriteFrameCache(); //获得SpriteFrame的缓存 5 pCache->addSpriteFramesWithFile("attack.plist"); 6 CCSprite* pSprite = CCSprite::createWithSpriteFrameName("A1_0.png"); //用attack.plist中的第一张图片名字创建一个精灵 7 pSprite->setPosition(ccp(size.width/2,size.height/2)); //设置精灵的坐标 8 9 10 char name[20]; 11 for(int i=0; i<4; i++) //将plist中的图片都加载,放到array中 12 { 13 sprintf(name, "A1_%d.png", i); 14 CCSpriteFrame* pframe = pCache->spriteFrameByName(name); 15 pArray->addObject(pframe); 16 } 17 18 CCAnimation* pAnimation = CCAnimation::createWithSpriteFrames(pArray,0.2f); //用array中的spriteframe创建一个animation,每帧间隔0.2s 19 CCAnimate* panimate = CCAnimate::create(pAnimation); 20 21 pSprite->runAction(CCRepeatForever::create(panimate)); 22 23 this->addChild(pSprite);