[以前的文章,重新发布]
(NO.3被无缘无故删除了)
(吐槽一下CSDN,删除文章没有解释没有通知,直接就没了)
这篇文章介绍一下动画效果,我先拿棋子翻动效果举个例子
1.生成精灵表
这是我在3dmax渲染出来的棋子翻动动画,一共有11张图(其他图是后面加的),每张图下面都加了棋面(这里偷了懒)
再用TexturePackerGUI.exe这个软件将分开的图片生成一张图片和一个plist文件,这样我们需要的精灵表就完成了
2.加载精灵表
SpriteBatchNode* spritebatch = SpriteBatchNode::create("chess.png");
SpriteFrameCache *frameCache=SpriteFrameCache::sharedSpriteFrameCache();
frameCache->addSpriteFramesWithFile("chess.plist");
现在好像不太建议使用SpriteBatchNode,我现在能做到的是使用,还无法理解里面的内容
3.创建动画
Vector<SpriteFrame*> action_change;//保存帧
char str[100]={0};
if(chess_turn==-1)
{
for(int i=0;i<=10;i++)
{
sprintf(str,"chess_%d.png",i);
//SpriteFrame * sf=SpriteFrame::create(str,Rect(0,0,60,60)); //通过文件
SpriteFrame* sf = frameCache->spriteFrameByName( str ); //通过<span style="font-size:10px;">精灵表</span>
action_change.pushBack(sf);//存入Vector
}
}
上面有两种方法,一种是直接从文件中读取,一种是读取精灵表.
其中的区别很明显,如果读取文件,那么这一次绘图需要读取11此,而读取精灵表只需读取1次,可以很大程度上节省资源
(还有一种情况,也许读取一个整合了全部图片的大图片所需的时间比读取单个图片的时间大很多,现在不去深究~)
4.添加动画
Animation * ani=Animation::createWithSpriteFrames(action_change,0.1);
ani->setDelayPerUnit(0.50f / 11.0f);//这个动画包含11帧,将会持续0.5秒
auto sprite=Sprite::create();
Action * act=Sequence::create(
//动画
Animate::create(ani),
//删除自己
CallFuncN::create(sprite,callfuncN_selector(Chess::killMe)),
NULL);
sprite->setTag(50);
sprite->setAnchorPoint(Point::ZERO);
sprite->runAction(act);
this->addChild(sprite);
5.释放资源
void Chess::killMe(Node * pSender)//删除自己
{
pSender->removeFromParentAndCleanup(true);
}
经过以上代码就能实现在棋盘上绘制棋子翻转的动画效果了