Last time I said how to play the card flip animation, the card turned to the front, you need to let the player drag the card to the exit area or graveyard area.
Here is a repeat of the previous content:
1. Heavy-duty Touch function:
Virtualbool Ontouchbegan (touch* Touch, event* event); virtual void ontouchmoved (touch* touch, event* event); virtual void O ntouchended (touch* Touch, event* Event);
2. Add in init:
-------------------------------------------------------------add mouse to monitor listener->setswallowtouches (true);// Guaranteed to touch only one sprite Listener->ontouchbegan = Cc_callback_2 (Game::ontouchbegan, this); listener->ontouchmoved = CC_ Callback_2 (game::ontouchmoved, this); listener->ontouchended = Cc_callback_2 (game::ontouchended, this); _ Eventdispatcher->addeventlistenerwithscenegraphpriority (listener, this);
OK, now start implementing the Mobile Card function:
1. Let's say we have selected a flipped card (the card that was not flipped cannot be moved):
BOOL Game::ontouchbegan (touch* Touch, event* Event) {point touchpoint=touch->getlocation (); This->selectspritefortouch (touchpoint); return true;}
void Game::selectspritefortouch (Point touchpoint) {player* player;//is more than the last player's pointer, because a game has two players, each player has its own card if (who_turn= =1) {player=player_1;} else{player=player_2;} int x,y;x= (int) touchpoint.x;y= (int) touchpoint.y; for (Opencard *sprite:player->opensprites)//Traverse the Card Wizard in the container {if (sprite->card_istouched (x, y) && sprite-> Isopend==0 && (player->player_magic_now>=sprite->need_magic)//If the area of the Click Exists Wizard & this Genie has not been opened {Cclog ("Open a Card"); Istouchcard=1;sprite->card_set_opencard ();p layer->player_magic_now-=sprite->need_magic; Print_players_message ();sprite->isopend=1; Open_sel = Sprite; Break }else if (sprite->card_istouched (x, y) && sprite->isopend==1 && sprite->touchable==1)// This is the focus of this time, mobile card {istouchmove=1;//can be moved after flipping sprite->card_show_message (true);//Mobile display card information sprite->card_setsize _big (2);//When moving the card becomes larger 2sprite->setzorder (100);//The sprite pointer that overrides the other Sprite open_sel=sprite;//action points to this Genie break;} }}
2. Now is the mobile card:
void Game::ontouchmoved (touch* Touch, event* Event) {if (istouchmove==1 && open_sel->touchable==1) {int x,y;x = (int) touch->getlocation (). x;y= (int) touch->getlocation (). y;open_sel->card_set_position (x, y);}}
This allows the card to follow the touch point movement.
3. But the game can not only do the above two steps, when we move the card to different places when the card needs to do different actions, I divided into 3 parts
1) Move to illegal position, card automatically back to hand
2) move to the card area, automatic alignment position
3) Move to the cemetery, the card automatically aligns the position and closes the card
void game::ontouchended (touch* Touch, event* Event) {player* player;if (who_turn==1) {player=player_1;} else{player=player_2;} int Locate_try=0;if (istouchmove==1) {if (((Locate_try=open_sel->card_judge_is_out ())!=0) && Had_card ( Locate_try,open_sel->card_type)//If the card is moved to the out-of-hand area {open_sel->card_home_to_out (locate_try);//card moves to the attempt area Open_ The sel->card_loc_num=locate_try;//card position number equals the area number of the attempt open_sel->card_setsize_small ();//card Recovery size Open_sel->card_ Show_message (false);//The card does not display information when moving auto Playcard=cardsprite::create (open_sel->card_loc_num,open_sel->card_ num);//Create a Game Card wizard (different from the card sprite that played the animation before) point Point=playcard->card_get_locate (Open_sel->card_loc_num); Cclog ("Create a cardsprite in%f,%f", Point.x,point.y);p laycard->setposition (point);p Laycard->setglobalzorder (0); This->addchild (Playcard);p layer->cardsprite.pushback (Playcard);} else if (Open_sel->card_home_to_die ())//if moved to cemetery {player->player_magic_now+=open_sel->need_magic/2;// The player restores the Magic Print_players_message () which is normally opened by this card; Open_seL->card_setsize_small (); Open_sel->card_show_message (false);} Else{open_sel->card_setsize_small (); Open_sel->card_show_message (false); Open_sel->setzorder (0); Open_ Sel->card_go_home ();//Cards fight back card}}open_sel = null;istouchcard=0;istouchmove=0;}
There's a repetition here and before, no elaboration.
Cocos2d-x 3.3 Card Design no.3 card mobile