//获取屏幕大小
Size visibleSize = Director::getInstance()->getVisibleSize();
//auto size = Director::getInstance()->getWinSize();
//帧动画的创建
//方式一,通过多张图片来创建
auto
sprite1 = Sprite::create(
"grossini_dance_05.png"
);
sprite1->setPosition(Vec2(visibleSize.width*0.3, visibleSize.height/2));
this
->addChild(sprite1);
//创建帧动画序列,名词形式
auto
animation = Animation::create();
for
(
int
i=5; i<11; i++)
{
char
szName[100] = {0};
sprintf
(szName,
"grossini_dance_%02d.png"
, i);
animation->addSpriteFrameWithFile(szName);
}
//设置帧动画属性
animation->setDelayPerUnit(2.0f / 6);
//每一帧停留的时间,2秒时间完成6幅图片显示,切记要写成2.0f形式!
animation->setRestoreOriginalFrame(
true
);
//播放完后回到第一帧
auto
animate = Animate::create(animation);
sprite1->runAction(RepeatForever::create(animate));
//方式二,通过一张集合的图片来创建
//创建2D纹理
auto texture = Director::getInstance()->getTextureCache()->addImage("dianqiu.jpg");
//建立图片帧
auto frame0 = SpriteFrame::createWithTexture(texture, Rect(259*0, 258*0, 259, 258));
auto frame1 = SpriteFrame::createWithTexture(texture, Rect(259*1, 258*0, 259, 258));
auto frame2 = SpriteFrame::createWithTexture(texture, Rect(259*2, 258*0, 259, 258));
auto frame3 = SpriteFrame::createWithTexture(texture, Rect(259*0, 258*1, 259, 258));
auto frame4 = SpriteFrame::createWithTexture(texture, Rect(259*1, 258*1, 259, 258));
auto frame5 = SpriteFrame::createWithTexture(texture, Rect(259*2, 258*1, 259, 258));
Animation* dianqiu = Animation::create();
dianqiu->addSpriteFrame(frame0);
dianqiu->addSpriteFrame(frame1);
dianqiu->addSpriteFrame(frame2);
dianqiu->addSpriteFrame(frame3);
dianqiu->addSpriteFrame(frame4);
dianqiu->addSpriteFrame(frame5);
dianqiu->setDelayPerUnit(0.1f);
dianqiu->setRestoreOriginalFrame(true);
Animate* dianqiuaciton = Animate::create(dianqiu);
Sprite* sp1 = Sprite::create();
sp1->setPosition(Point(300,300));
this->addChild(sp1);
sp1->runAction(RepeatForever::create(dianqiuaciton));
//方式三,通过.plist 文件来创建
auto
cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile(
"animate.plist"
);
char buff[16];
Animation* pAnimation1 = Animation::create();
//设置间隔时间
pAnimation1->setDelayPerUnit(0.1f);
//设置播放完毕回到第一帧
pAnimation1->setRestoreOriginalFrame(true);
for (int id = 1; id <= 8; id++) {
sprintf(buff, "a_00%d.png", id);
pAnimation1->addSpriteFrame(
SpriteFrameCache::getInstance()->getSpriteFrameByName(buff));
}
AnimationCache::getInstance()->addAnimation(pAnimation1, "Enemy1Blowup");
Animation * anima = AnimationCache::getInstance()->getAnimation("Enemy1Blowup");Animate * action1 = Animate::create(anima);