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

cocos2dx创建精灵动作

2014-06-25 17:52 344 查看
玩游戏的都知道,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);//精灵添加动作




最后结果如上图所示!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: