借助CCAction实现转盘抽奖

来源:互联网 发布:ubuntu挂载新硬盘 编辑:程序博客网 时间:2024/05/16 17:08

     借助cocos2dx自带的CCMoveBY和CCEaseInOut实现目前流行的转盘抽奖效果。

     创建一个辅助结点node_projection_,将转盘中被选中的精灵的序号pos与node_projection_的X坐标建立映射关系。当node_projection_执行CCMoveBY时,通过在每帧执行的回调函数roll_update(float dt)去刷新转盘中精灵的状态。而node_projuction_在X轴上的移动的距离决定了转盘最终高亮的位置。

 1 void LayerRoll::roll(CCObject* pSender, CCControlEvent event) 2 { 3      4     node_projection_->setPosition(ccp(0, 0)); 5     spr_num_ = 5; 6     roll_index_ =1+ rand() % 5; 7      8     int rount_t = 4; //round_time 周期数 9     roll_unit_l_ = 32;  //node_projection移动单元的长度10     int length = roll_unit_l_*spr_num_*rount_t + roll_unit_l_*roll_index_;11   12     CCCallFunc* mend = CCCallFunc::create(this, callfunc_selector(LayerRoll::roll_end));13     CCMoveBy* mov = CCMoveBy::create(4.0f, ccp(length, 0));14     15     CCSequence* seq = CCSequence::create(CCEaseInOut::create(mov,1.5f), mend, NULL); //CCEaseInOut 慢快慢效果16     node_projection_->runAction(seq);17     schedule(schedule_selector(LayerRoll::roll_update));18     19 }20 21 void LayerRoll::roll_end()22 {23     24     unschedule(schedule_selector(LayerRoll::roll_update));25 }26 27 void LayerRoll::roll_update(float dt)28 {29     int pos = node_projection_->getPositionX();30     pos = pos % (roll_unit_l_*spr_num_); 31     pos = pos / roll_unit_l_+1;32     if (pos == 1)33     {34         spr_1_->setScale(0.8f);35     }36     else37     {38         spr_1_->setScale(0.6f);39     }40     if (pos==2)41     {42         spr_2_->setScale(0.8f);43 44     }45     else46     {47         spr_2_->setScale(0.6f);48 49     }50     if (pos==3)51     {52         spr_3_->setScale(0.8f);53 54     }55     else56     {57         spr_3_->setScale(0.6f);58 59     }60     if (pos == 4)61     {62         spr_4_->setScale(0.8f);63 64     }65     else66     {67         spr_4_->setScale(0.6f);68 69     }70     if (pos == 5)71     {72         spr_5_->setScale(0.8f);73 74     }75     else76     {77         spr_5_->setScale(0.6f);78 79     }80 }

这里转盘的格数为5

0 0