昆特牌在整个游戏中需要三种触摸形式,一种是初始发牌时的换牌阶段,点击可以更换牌(镶嵌到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);
//
//将被点击卡重新加入牌组中,并从牌组中拿出一张卡
}
}
}