少年啊,來一發昆特嗎(二)——昆特牌的觸摸

昆特牌在整個遊戲中需要三種觸摸形式,一種是初始發牌時的換牌階段,點擊可以更換牌(鑲嵌到tableview中),二是正常打牌階段的觸摸,點擊兩次後,將牌打出,第三種是醫生的復活技能效果,選擇墓地中的牌將其復活到場上,所以在上一節使用了3種觸摸函數來達成這三種效果,以及npc的禁止點擊的種類,所以採用了在onEnter中通過所需要的BType的值來確定觸摸函數

void GWentCard::onEnter(){
    CCLayer::onEnter();//一定要用基類的onEnter,否則可能出現各種問題
    //觸摸響應註冊
    touchListener = EventListenerTouchOneByOne::create();//創建單點觸摸事件監聽器
    this->setTouchEnabled(true);
    //當爲0時就使用如下觸摸
    if(Spritetype==0){
      touchListener->onTouchBegan = CC_CALLBACK_2(GWentCard::onTouchBegan, this);//觸摸開始
      touchListener->onTouchMoved = CC_CALLBACK_2(GWentCard::onTouchMoved, this);//觸摸移動
      touchListener->onTouchEnded = CC_CALLBACK_2(GWentCard::onTouchEnded, this);//觸摸結束
      touchListener->setSwallowTouches(false);//不向下吞併觸摸
      _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);//註冊分發器
    }else if(Spritetype==1){
       touchListener->onTouchBegan = CC_CALLBACK_2(GWentCard::onTouchBeganSelect, this);//觸摸開始
       touchListener->onTouchMoved = CC_CALLBACK_2(GWentCard::onTouchMovedSelect, this);//觸摸移動
       touchListener->onTouchEnded = CC_CALLBACK_2(GWentCard::onTouchEndedSelect, this);//觸摸結束
       touchListener->setSwallowTouches(false);//不向下吞併觸摸
       _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);//註冊分發器

    }else if(Spritetype==2){
         touchListener->onTouchBegan = CC_CALLBACK_2(GWentCard::onTouchBeganSelectGrave, this);//觸摸開始
         touchListener->onTouchMoved = CC_CALLBACK_2(GWentCard::onTouchMovedSelectGrave, this);//觸摸移動
         touchListener->onTouchEnded = CC_CALLBACK_2(GWentCard::onTouchEndedSelectGrave, this);//觸摸結束
         touchListener->setSwallowTouches(false);//不向下吞併觸摸
         _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);//註冊分發器

    }else if(Spritetype==3){

    }
    String *pow=String::createWithFormat("%d",this->getNewPower());
    auto label=LabelTTF::create(pow->getCString(),"",50);
    label->setAnchorPoint(Point(0.5,0.5));
    label->setName("power");
    Size size=this->getContentSize();
    label->setPosition(Point(32,size.height-30));
    label->setColor(Color3B(255,0,0));
    this->addChild(label,1);
}

下面簡要挑一個觸摸函數來說明,下面的函數是爲了在發牌階段使用的觸摸函數,主要目的是點擊後進行換牌,cocos2dx的觸摸流程在上一篇中已經介紹過了,這裏就不多說了,因爲這個函數是用在tableview上的,所以必須保證在滑動時不觸發點擊效果,保證方法很容易,也就是在began中不進行處理,在end中如果點擊位置和在began中的位置一樣時,就進行處理點擊效果,因爲滑動後處於end位置的點必然和初始點擊位置不同,在初始保存點擊位置,於end中獲取結束位置,比較兩者如果一致,就觸發點擊效果,否則就不處理,之後的過程就是常規的換牌過程,就不多作解釋了


bool GWentCard::onTouchBeganSelect(CCTouch *pTouch, CCEvent *pEvent){
    firstPoint=pTouch->getLocation();
    return true;
}
void GWentCard::onTouchMovedSelect(CCTouch *pTouch, CCEvent *pEvent){


}
void GWentCard::onTouchEndedSelect(CCTouch *pTouch, CCEvent *pEvent){
    auto EndPoint=pTouch->getLocation();
    if(firstPoint!=EndPoint){
        return;
    }
    TableViewCell *cell=(TableViewCell *)this->getParent();
    TableViewCell *targetcell=(TableViewCell *)pEvent->getCurrentTarget()->getParent();
    int id=cell->getIdx();
    int idx=targetcell->getIdx();
    auto table=(TableView *)m_gameMain->getChildByTag(0);//cell需要獲得兩次parent才能拿到table
    CCPoint ptouch=table->convertToNodeSpaceAR(pTouch->getLocation());
    auto distance=(int)(ptouch.x-table->getContentOffset().x)/690;//獲得點擊的頁數即第幾個cell
    Size size=this->getContentSize();
    Point p=this->getPosition();
    CCRect rect(p.x,p.y,size.width,size.height);
    log("%f",table->getContentOffset().x);
    if(cell->getIdx()==distance){
       auto dis=(-(int)(table->getContentOffset().x))/690;
       auto s=-(int)(table->getContentOffset().x)-distance*690;
       //auto m=690-s;
       ptouch.x=ptouch.x+s;
      // log("minx=%d,miny=%d,maxx=%d,maxy=%d",this->getBoundingBox().getMinX(),this->getBoundingBox().getMinY(),this->getBoundingBox().getMaxX(),this->getBoundingBox().getMaxY());
       if(rect.containsPoint(ptouch)){
           Array *deckarr= m_gameMain->getDeckArray();//獲取牌組數據
           Dictionary *dic=(Dictionary *)deckarr->randomObject();//獲取一個隨機數
            int time= m_gameMain->getRetime();
            time=time+1;
            m_gameMain->setRetime(time);
           GWentCard *card=GWentCard::create(dic);
           card->setBType(1);
           card->setPosition(this->getPosition());
           card->setGameMain(this->getGameMain());
           card->setAnchorPoint(Point::ZERO);
           deckarr->addObject(this->dictionary);//加入牌組中
           m_gameMain->getHandArray()->addObject(dic);//加入手牌中   
           m_gameMain->getHandArray()->removeObject(this->dictionary);//從手牌中刪除
           m_gameMain->setSelect(false);
           deckarr->removeObject(dic);//從牌組中刪除  

           cell->removeChild(this);
           cell->addChild(card);
           card->getGameMain()->updateretimeslabelinfo(0);
//         
          //將被點擊卡重新加入牌組中,並從牌組中拿出一張卡
       }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章