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

从Delphi开始学Cocos2dx-3.0[13]:动画-直接从图片文件创建动画

2013-12-28 14:01 441 查看
先找个6帧的动画



//====================================================================================

 












//=====================================================================================

命名成 1 2 3 4 5 6 .png

// ***添加一张精灵图片, 这里要做动画,就不指定文件名了
auto sprite = TSprite::create(); // <- 注意这里

// 设置位置到正中间
sprite->setPosition(g_ClientMidPoint);

// 添加到Helloworld图层
this->addChild(sprite, 0, 1000);


bool THelloWorld::onTouchBegan(TTouch* touch, TEvent* event)
{
auto sprite =  (TSprite*)(this->getChildByTag(1000));

//sprite->stopAllActions();
sprite->cleanup();
sprite->runAction(TPlace::create(TPoint(100.0f, g_ClientMidY)));

auto animation = TAnimation::create();
// 转载图片
for (int i = 1; i <= 6; i++)
{
char szName[260] = {0};
sprintf(szName, "%d.png", i);
animation->addSpriteFrameWithFileName(szName);
}

// 设置动画播放的属性 2秒 6帧
animation->setDelayPerUnit(1.2f / 6.0f);

// 设置精灵帧的使用方式, 做完动画还原成初始帧
animation->setRestoreOriginalFrame(false);

// 重复10次
animation->setLoops(3);

// 创建动画动作
auto action = TAnimate::create(animation);

sprite->runAction(TSpawn::create(action, TMoveBy::create(1.2f * 3,TPoint(500.0f, 0.0f)), NULL));

CCLOG("THelloWorld::onTouchBegan id = %d, x = %f, y = %f", touch->getID(), touch->getLocation().x, touch->getLocation().y);
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: