做了一个水果转盘,飞禽走兽的旋转特效;效果如下:
下面说一下实现的思路:使用定时器移动亮块的位置;定时器先让亮块启动,并逐步加速,然后是高速的转动,在慢慢的减速,最后在指定的地方停下来。
一开始是创建一个亮块,然后使用MoveTo来实现;但是效果不太好,当速度很快的时候,亮块会漂移到外部的区域,没有按照规定的轨迹来进行移动。
//----1.精灵转动----
int m_iStarIndex;
int m_runTime;
void runPlay();
void runAct(float time);
//----精灵转动----
void LoginScene::runPlay()
{
//1.
m_runTime = 0;
this->schedule(schedule_selector(LoginScene::runAct), 1.0f);
//2.
}
//----1.精灵转动----
void LoginScene::runAct(float time)
{
float t1 = 0.8f; //1--开始启动
float t2 = 0.6f; //1--开始加速
float t3 = 0.2f; //2--快速匀速
float t4 = 0.6f; //3--开始减速
float t5 = 0.8f; //3--慢慢停止
m_runTime++;
log("----m_runTime,%d", m_runTime);
if (m_iStarIndex >= 28 || m_iStarIndex < 0)
{
m_iStarIndex = 0;
}
m_iStarIndex++;
//起始位置
char imgName[10] = { 0 };
sprintf(imgName, "img_run%d", m_iStarIndex);
Sprite* sp = static_cast<Sprite *>(nodeRun->getChildByName("sp_run"));
ImageView* iv = static_cast<ImageView *>(nodeRun->getChildByName(imgName));
Vec2 vStart = sp->getPosition();
Vec2 vEnd = iv->getPosition();
MoveTo* mt = MoveTo::create(0.5f, vEnd);
sp->runAction(Sequence::create(DelayTime::create(0.01f), mt, NULL));
if (m_runTime == 2)
{
this->unschedule(schedule_selector(LoginScene::runAct));
this->schedule(schedule_selector(LoginScene::runAct), t2);
}
else if (m_runTime == 10)
{
this->unschedule(schedule_selector(LoginScene::runAct));
this->schedule(schedule_selector(LoginScene::runAct), t3);
}
else if (m_runTime == 40)
{
this->unschedule(schedule_selector(LoginScene::runAct));
this->schedule(schedule_selector(LoginScene::runAct), t4);
}
else if (m_runTime == 48)
{
this->unschedule(schedule_selector(LoginScene::runAct));
this->schedule(schedule_selector(LoginScene::runAct), t5);
}
else if (m_runTime > 50 && m_iStarIndex==1)
{
this->unschedule(schedule_selector(LoginScene::runAct));
m_runTime = 0;
}
}
后来发现问题了,换了另一个实现方法:创建好全部的亮块,并先隐藏;通过定时器来控制显示和隐藏。最终通过调整达到了预期的效果。下面是实现的代码。
//----2.精灵转动----
bool m_bCreate;
int m_maxSize;
int m_curID;
int m_runNum;
int m_endID;
void createBox();
void runTurnBox(float time);
void switchBox(int boxID, bool isShow);
void setEndIndex(int idx);
void testRunBox();
void randEndIndex();
//--------------------------------------------------------------
//创建好转动的亮块
void LoginScene::createBox()
{
m_runNum = -1;
m_curID = 0;
m_maxSize = 2