您的位置:首页 > 移动开发 > Cocos引擎

Cocos2D-x游戏开发之十六:创建会运动的精灵

2014-04-01 22:10 507 查看
今天我们试着让我们的精灵动起来,当然我们知道所谓的动就是让精灵在多副图之间切换,所以我们开始之前先准备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();


这样就完成了最后总的结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐