標籤:cocos2d-x 卡牌 觸控 遊戲 移動
上次說了如何播放卡牌翻轉的動畫,卡牌翻到正面後,就需要讓玩家將卡牌拖拽至出場地區或者墓地地區了。
這裡重複一下之前的內容:
1.重載觸控函數:
virtualbool onTouchBegan(Touch* touch, Event* event);virtual void onTouchMoved(Touch* touch, Event* event);virtual void onTouchEnded(Touch* touch, Event* event);
2.在init裡添加:
//-------------------------------------------------------------添加滑鼠監聽listener->setSwallowTouches(true);//保證同時只能觸摸到一個精靈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);
好了,現在開始實現移動卡牌的功能:
1.假設我們已經選取了翻轉過的卡牌(未翻轉前的卡牌不能移動):
bool Game::onTouchBegan(Touch* touch, Event* event){ Point touchpoint=touch->getLocation(); this->selectSpriteForTouch(touchpoint); return true;}
void Game::selectSpriteForTouch(Point touchpoint){Player* player;//比上次多了玩家的指標,因為一局遊戲有兩個玩家,每個玩家都有自己的卡牌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)//在容器中遍曆卡牌精靈 {if (sprite->card_isTouched(x,y) && sprite->isOpend==0 && (player->player_magic_now>=sprite->need_magic))//如果點擊的地區存在精靈&這個精靈沒有被翻開過 {CCLOG("open a card");isTouchCard=1;sprite->card_set_openCard();player->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)//這裡是這次的重點,移動卡牌{isTouchMove=1;//翻轉後就可以移動sprite->card_show_message(true);//移動的時候顯示卡牌的資訊sprite->card_setsize_big(2);//移動的時候卡牌變大2sprite->setZOrder(100);//覆蓋其他精靈Open_sel=sprite;//操作的精靈指標指向這個精靈break;} }}
2.現在是移動卡牌:
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);}}這樣就能實現卡牌跟隨觸摸點移動了。
3.但是遊戲不能只做到上面兩步,當我們把卡牌移動到不同地方的時候卡牌需要做不同的動作,我分成了3個部分
1)移動到非法位置,卡牌自動回到手牌
2)移動到出牌地區,卡牌自動對準位置
3)移動到墓地,卡牌自動對準位置,並關閉卡牌
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))//如果卡牌移動到了出牌地區{Open_sel->card_home_to_out(locate_try);//卡牌移動到嘗試地區Open_sel->card_loc_num=locate_try;//卡牌位置編號等於嘗試的地區編號Open_sel->card_setsize_small();//卡牌恢複大小Open_sel->card_show_message(false);//卡牌不顯示移動時的資訊auto playcard=CardSprite::create(Open_sel->card_loc_num,Open_sel->card_num);//建立遊戲卡牌精靈(與之前播放動畫的卡牌精靈不同)Point point=playcard->card_get_locate(Open_sel->card_loc_num);CCLOG("create a cardsprite in %f,%f",point.x,point.y);playcard->setPosition(point);playcard->setGlobalZOrder(0);this->addChild(playcard);player->cardSprite.pushBack(playcard);}else if(Open_sel->card_home_to_die())//如果移動到了墓地{player->player_magic_now+=Open_sel->need_magic/2;//玩家恢複一般翻開這張卡牌的魔法print_players_message();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();//卡牌回手牌}}Open_sel = NULL;isTouchCard=0;isTouchMove=0;}
這裡和之前的有重複,就不做闡述了
cocos2d-x 3.3 之卡牌設計 NO.3 卡牌移動