Realize a four rotor, each rotor has five suits of old * Tiger machine. The principle of rotation of the rotor is very simple, and the background graph infinite scrolling principle is the same: The row of the wizard on the screen upward scrolling, and then cut through the cutting node to create the effect of the rotor rolling.
1 voidLayerslotmachine::on_btn_roll (ccobject* Psender, cccontroleventEvent)2 {3 if(!flag_act_)4 {5 Schedule (Schedule_selector (xlayerslotmachine::roll_update));6Stop_flag_ =0;7 intTarget_index;8 floatMov_dis;9 TenTarget_index = rand ()%5; OneCclog ("slot1:%d", Target_index); AMov_dis =Get_mov_dis (Target_index, cur_index1_); -Cur_index1_ =Target_index; - Roll_act_start (array_1_, Mov_dis); the -Target_index = rand ()%5; -Cclog ("slot2:%d", Target_index); -Mov_dis =Get_mov_dis (Target_index, cur_index2_); +Cur_index2_ =Target_index; - Roll_act_start (array_2_, Mov_dis); + ATarget_index = rand ()%5; atCclog ("slot3:%d", Target_index); -Mov_dis =Get_mov_dis (Target_index, cur_index3_); -Cur_index3_ =Target_index; - Roll_act_start (array_3_, Mov_dis); - -Target_index = rand ()%5; inCclog ("slot4:%d", Target_index); -Mov_dis =Get_mov_dis (Target_index, cur_index4_); tocur_index4_ =Target_index; + Roll_act_start (array_4_, Mov_dis); - theFlag_act_ =true; * } $ }Panax Notoginseng - voidLayerslotmachine::roll_act_start (ccarray* Aray,floatMov_dis) the { +ccobject*obj; A Ccarray_foreach (Aray, obj) the { +cccallfunc* act_end = Cccallfunc::create ( This, Callfunc_selector (xlayerslotmachine::roll_act_end)); -ccsequence* seq1 = ccsequence::create (Ccmoveby::create (Mov_dis/rotate_v_, CCP (0, Mov_dis)) $ , Act_end $ , NULL); -ccsprite* SPR = (ccsprite*) obj; -Spr->runaction (seq1); the } - }Wuyi the voidLayerslotmachine::roll_update (floatDT) - { WuRoll_bound_test (array_1_,node_slot_1_,1); -Roll_bound_test (Array_2_,node_slot_2_,2); AboutRoll_bound_test (array_3_,node_slot_3_,3); $Roll_bound_test (array_4_,node_slot_4_,4); - } - - voidlayerslotmachine::roll_act_end () A { +++Stop_flag_; the if(stop_flag_==slot_length_*4) - { $Flag_act_ =false; the Unschedule (Schedule_selector (xlayerslotmachine::roll_update)); the } the}
There are two issues to be aware of:
(1) Calculation of moving distance
1 floatLayerslotmachine::get_mov_dis (intTarget_index,intCur_index)2 {3 floatMov_dis;4 floatSlot_height = spr_model_->getcontentsize (). Height;5 intT =7;6 if(Target_index >Cur_index)7 {8Mov_dis = slot_height* (slot_length_*t + (Target_index-cur_index));9 }Ten Else One { AMov_dis = slot_height* (slot_length_*t + (Slot_length_-(Cur_index-( Target_index) )); - } - returnMov_dis; the}
T: Number of cycles
Slot_height: Height of the rotor
Slot_length: The number of suits on a single rotor, this example is 5
Target_index: Target Location
Cur_index: Current Location
Here, the moving distance is calculated in two cases: when Target_index>cur_index, the current period can be moved to the specified position, the number of cells to be moved (Target_index-cur_index);
When Target_index<cur_index, as the rotor can only scroll upward, so in order to reach the specified position in the next cycle, the number of cells to be moved is
((Slot_length_-(Cur_index-target_index))).
(2) Processing of rotor rolling
1 voidLayerslotmachine::roll_bound_test (ccarray* arry,ccnode* Node_slot,intslot_num)2 {3ccobject*obj;4 floatBound_y = Spr_model_->getcontentsize (). height* (Slot_length_-2);5 6 Ccarray_foreach (Arry, obj)7 {8ccsprite* SPR = (ccsprite*) obj;9 Ten if(Spr->getpositiony () >bound_y) One { A if(Spr->gettag ()- +*slot_num = =0) - { -ccsprite* spr0 = (ccsprite*) Node_slot->getchildbytag ( +*slot_num + Slot_length_-1); theSpr->setpositiony (Spr0->getpositiony ()-spr_model_->getcontentsize (). height); - } - Else - { +ccsprite* spr0 = (ccsprite*) Node_slot->getchildbytag (Spr->gettag ()-1); -Spr->setpositiony (Spr0->getpositiony ()-spr_model_->getcontentsize (). height); + } A } at } -}
When the sprite position is above the boundary height Boundy, move the sprite to the bottom of the previous sprite for circular scrolling. It is important to note that the position of the previous sprite should be used as a benchmark, otherwise an offset will be generated.
cocos2d realization slotmachine (OLD * Tiger * machine)