今天我们试着让我们的精灵动起来,当然我们知道所谓的动就是让精灵在多副图之间切换,所以我们开始之前先准备15张精灵运动图片,然后用工具将这些图片打包。所以当我们准备好这一切之后就可以开始编码了。
void Player::run()
{
CCSpriteFrameCache * freamCache = CCSpriteFrameCache::sharedSpriteFrameCache();
freamCache->addSpriteFramesWithFile("run.plist","run.png");
CCSpriteFrame *frame = NULL;
CCArray *freamlist =CCArray::create();
for (int i =1; i <= 15 ; i++)
{
frame = freamCache->spriteFrameByName(CCString::createWithFormat("run%d.png",i)->getCString());
freamlist->addObject(frame);
}
CCAnimation *anination = CCAnimation::createWithSpriteFrames(freamlist);
anination->setLoops(-1);
anination->setDelayPerUnit(0.08f);
CCAnimate *animate = CCAnimate::create(anination);
m_sprite->runAction(animate);
}
我们在我们之前已经有的Player角色类中添加一个Run函数,然后在场景中创建完player对象之后调用run函数,我们的精灵就可以动起来了。
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCSprite *sprite = CCSprite::create("player.png");
Player *player = Player::create();
player->BindSprite(sprite);
map->addChild(player);
CCTMXObjectGroup * objgroup=map->objectGroupNamed("player");
CCDictionary * playerpoint =objgroup->objectNamed("playerpoint");
float x = playerpoint->valueForKey("x")->floatValue();
float y = playerpoint->valueForKey("y")->floatValue();
player->setPosition(ccp(x,y));
player->run();
这样就完成了最后总的结果: