/**
* 获得动画动作函数
* animName:动画帧的名字
* delay:动画帧与帧之间的间隔时间
* animNum:动画帧的数量
*/
Animation* Start::getAnimationByName(std::string animName,float delay,int animNum){
Animation* animation = Animation::create();
// 循环从精灵帧缓存中获取与图片名称相对应的精灵帧组成动画
for(unsigned int i = 1;i<=animNum;i++){
// 获取动画图片名称,例如plane0.png
std::string frameName = animName;
frameName.append(StringUtils::format("%d",i)).append(".png");
// 将单张图片添加为精灵帧(即动画帧)
animation->addSpriteFrameWithFile(frameName.c_str());
}
// 设置动画播放的属性
animation->setDelayPerUnit(delay);
// 让精灵对象在动画执行完后恢复到最初状态
animation->setRestoreOriginalFrame(true);
return animation;
}
Animation* laughAnimation = getAnimationByName("mole_laugh", 0.5f, 3);
AnimationCache::getInstance()->addAnimation(laughAnimation, "laughAnimation");
auto laughAnimation = Animate::create(AnimationCache::getInstance()->getAnimation("laughAnimation"));
mallet->setScale(0.8f);
mallet->setPosition(mole->getPosition().x+100, mole->getPosition().y+60);
this->addChild(mallet, 0);
// 播放木槌动画
mallet->runAction(Sequence::create(malletAnimation,
CallFunc::create([=]{
// 播放地鼠被打中音效
SimpleAudioEngine::getInstance()->playEffect("ow.caf");
// 地鼠被打中后播放粒子效果
auto aswoon = ParticleSystemQuad::create("aswoon.plist");
aswoon->setPosition(mole->getPosition().x, mole->getPosition().y);
this->addChild(aswoon);
// 删除木槌
this->removeChild(mallet);
}), NULL));
转载于:https://my.oschina.net/u/2276921/blog/531034