玩游戏的都知道,hero没有动作,无论是走路,还是打架,都像站在滑板车上,像鬼魂一样平移的飘来飘去,那是一种令人何其惊悚的景象呀!
所以在精灵设计的时候,我们需要给精灵添加一定的动作,比如走路时,脚动,打架时,手舞足蹈,打有打姿,跑有跑姿,这样才够自然,因此我们需要用到cocostudio工具来生成plist文件,再使用这些资源配置来给精灵设置动作。(ps:cocostudio到官方网站下载对应系统的版本,至于使用,网上有很多资料)
话不多说了,开始这篇博客的旅程,
CCSprite *arrow = CCSprite::create("dammer0.png");//创建精灵初始状态
CCSpriteFrameCache *arrowCache = CCSpriteFrameCache::sharedSpriteFrameCache();
arrowCache->addSpriteFramesWithFile("dammer.plist","dammer.png");//加载plist文件
CCSpriteFrame * arrowFrame = NULL;
CCArray *arrowArray = CCArray::create();
for(int i=1;i<3;i++)
{
arrowFrame = arrowCache->spriteFrameByName(CCString::createWithFormat("dammer%d.png",i)->getCString());
arrowArray->addObject(arrowFrame);//添加帧动画
}
CCAnimation *arrowAnimation = CCAnimation::createWithSpriteFrames(arrowArray);//创建帧动画
arrowAnimation->setLoops(1);//设置帧动画动作循环次数,-1为无限循环
arrowAnimation->setDelayPerUnit(0.08f);//设置每帧动画的时延
CCAnimate *arrowAnimate = CCAnimate::create(arrowAnimation);//创建动画动作
arrow->runAction(arrowAnimate);//精灵添加动作
最后结果如上图所示!